MATLAB Answers

Creating a series of x-y plots

22 views (last 30 days)
Lin
Lin on 29 Sep 2014
Commented: Image Analyst on 3 Oct 2014
Hey, I want to make a set of x-y plots based on data with the following format:
"Tag Property-A Property-B
X1 A11 B11
X1 A12 B12
...........................
X2 A21 B21
..........................."
Properties A and B are the x and y variables. Each tag is matched with several pairs of A and B, and each plot should include data with a specific tag. Since there are a good many tags, it is impossible to manually separate the data. So is there any commands in Matlab that can be used to create such plots? I hope I have clearly stated my question. Thanks. Lin
P.S. I just realized the format couldn't display properly, so I'll briefly describe the data: there are three columns of data: Tag, Property A, Property B. Each tag encompasses a certain amount of x-y pairs, and the purpose is to make every one of the tags a plot showing the scattered x-y relationship.
A sample data file is attached.

  4 Comments

Show 1 older comment
Lin
Lin on 29 Sep 2014
Yes - the sample data are posted as an attachment. Thanks.
Lin
Lin on 29 Sep 2014
Thanks. This is helpful. I'm still trying to get a hang of the formatting rules here.

Sign in to comment.

Accepted Answer

Chad Greene
Chad Greene on 29 Sep 2014
If you have three columns, call them station, depth, and salinity, you could plot all the station 1 data as red pentograms, station 2 as blue squares, etc, like this:
plot(salinity(station==1),depth(station==1),'rp-');
hold on
plot(salinity(station==2),depth(station==2),'bs-');
plot(salinity(station==3),depth(station==3),'mo-');
plot(salinity(station==4),depth(station==4),'k^-');
plot(salinity(station==5),depth(station==5),'gx-');
legend('station 1','station 2','station 3','station 4',...
'station 5','location','southwest')
legend boxoff
box off
ylabel('depth (m)')
xlabel('salinity (psu??)')
set(gca,'ydir','reverse')
axis tight

  4 Comments

Show 1 older comment
Lin
Lin on 30 Sep 2014
Hey Chad, Thank you for the nice demonstration! It looks good to make all stations on a same plot when there are fewer stations. But it might be a bit of a problem of doing so for 70+ cruise stations, in which circumstance giving each station (or a couple of stations) their own plot will be needed. And I see there doesn't seem to be a solution to expect Matlab to collect data of a specific station and create plots automatically, as opposed to manually inputting station # in the commands. But 70 stations are not too bad to cope with, though typing may actually take quite a while. Anyway, thanks for spending time looking at this! Regards, Lin
Chad Greene
Chad Greene on 30 Sep 2014
Don't manually type plot lines 70 stations! If you want to do all of them on the same plot, this would work:
stationColors = jet(70); % creates 70 colors;
hold on;
for k = 1:70
plot(salinity(station==k),depth(station==k),...
'.-','color',stationColors(k,:));
end
box off
ylabel('depth (m)')
xlabel('salinity (psu??)')
set(gca,'ydir','reverse')
axis tight
Similarly, you could skip the loop and use scatter. You could even make the colors reflect something physically meaningful, rather than simply the station number. The profiles could be colored by latitude, or distance from the center of some gyre, or whatever interesting variable you're investigating.
If you only want to plot a single station, say, station number 15, you could use the example above like this:
k = 15; % <-- enter station number here
plot(salinity(station==k),depth(station==k),...
'.-','color',stationColors(k,:));
box off
ylabel('depth (m)')
xlabel('salinity (psu??)')
set(gca,'ydir','reverse')
axis tight
Lin
Lin on 3 Oct 2014
Thanks dude! I guess I can pretty much figure it out now. REALLY appreciate it!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Sep 2014
Just use readtable
t = readtable(xlsFullFileName);
Then extract the columns, use unique() to find unique tag names, then use logical indexing in a loop to extract rows for each tag in turn and plot. Let us know if you can't figure it out. Sorry, but I have to run off somewhere now.

  5 Comments

Show 2 older comments
Image Analyst
Image Analyst on 30 Sep 2014
If you have tags, like "X1", "X2", "ABC", "This stuff", "Tag 42", whatever, and some of them occur just once, and some of them multiple times, you can get a list of all the names, just listed once, with the unique() function.
uniqueNames = unique(tagList);
Once you have that you can get a list of all rows that have a particular tag, say "X1", like rows 1, 2, 14, 33, and 73 have tag "X1". Then you can get columns 2 and 3 for just those rows and do whatever you want with them.
Lin
Lin on 3 Oct 2014
OK, I think I get the point. I may need to dig it a little more later on, as I haven't figured out the commands to query the columns with a certain tag. But it's OK since I've done with the work by doing it manually. Thanks for the information anyway.

Sign in to comment.

Sign in to answer this question.

Tags


Translated by