Adding to plotyy / Datarepresentation using multiple representations of same data.
Show older comments
Hi, I have been having some trouble with a plot I want to make. The situation can be illustrated by the following simplified example:
clc
close all
clear all
a=[1:10]
b=3*a+5
c=a.^2
a_alt=[5 6]
c_alt=a_alt.^2
Magicplot=plotyy(a,c,a,b)
set(Magicplot(2),'ydir','reverse')
hold on
plot(a_alt,c_alt,'r*')
axes(Magicplot(2))
ylim([0 100]);
Now, this code makes me get the plot I want. However, I need a plot like this for a huge dataset. I have been trying to get it to work with an direct analog of this code, but my plot window keeps erasing whatever is related to the second y-axis, as soon as i start adding something.
My code is looking like:
TurnerPlot=plotyy(Dataset(:,1),Dataset(:,5),Dataset(:,1),Dataset(:,3))
set(TurnerPlot(2),'YDir','reverse')
axes(TurnerPlot(2)); ylabel('Dybde (m)')
axes(TurnerPlot(1)); ylabel('Kernetemperatur (C)')
ylim([10 30]);
title(TurnerPlotName)
hold on
Now I want to keep absolutely all the info on the plot, but add some red stars to it, just like my example does. The datapoints for the stars are a subset of the data making up the info on the left axis on my plotyy. You could call it the plotyy(1). How do i go about my business here? I find little help in the documentation, and could not find an example of this online.
Best Regards, Nicki Eriksen
Accepted Answer
More Answers (1)
dpb
on 20 Nov 2013
One solution
x=Dataset(:,1); % more convenient x/y-axis datasets
y=Dataset(:,5); % more convenient x/y-axis datasets
ystar=nan(size(x)); % a y-vector for the stars points initialized
xstar=(x==the_condition_that_sets_star_locations); % logical vector T for '*' locations
ystar(xstar)=y(xstar); % second dataset for the plot
hAx=plotyy(x,y,Dataset(:,1),Dataset(:,3))
hold on
plot(hAx(1),x,ystar,'r*') % the NaN's will be ignored by plot() so is your subset points
...
Finish the axes limits, etc., etc., etc., as desired
5 Comments
Nicki
on 20 Nov 2013
dpb
on 20 Nov 2013
OK, I did misunderstand your problem -- I thought it had to do w/ automagically selecting the point locations instead of manually.
As for why for your symptoms, there's too little information to tell conclusively.
A couple of possibilities--
a) use the handles optional input to hold to set it for both axes instead of only the current one. Also it only has any effect for commands subsequent to its execution so order is important. Note that in your sample code it appears before the plot statement that draws the stars whereas in the latter code it's at the end and therefore only anything from then on will be affected.
b) There's also the question of what ylim is for the two axes and how that compares to the actual data -- is it possible you've simply changed the displayed range to something other than where the data are?
I'd suggest using the script/function that fails but with a set of artificial data and see if it still provides the unwanted symptom. If so, post that so can duplicate it--that will undoubtedly lead to a solution (or, in building that example case you may uncover the culprit yourself :) )
Nicki
on 20 Nov 2013
Nicki
on 20 Nov 2013
See the answer for the cause/cure of your problem, just a comment on the above comment regarding hold
You're misreading the doc's -- not a difficult thing to do for a non-native English speaker.
"HOLD ON holds the current plot and all axis properties..."
The catch is that plot and axis are singular in the above, not plural, and so when it goes on to say "...so that subsequent graphing commands add to the existing graph." it is speaking of the one graph or axes object to which hold was applied, not to all axes in the current figure.
To apply hold to all axes, use the optional handle(s) argument--it accepts a vector of handles, not just a single one.
hold(TurnerPlot,'on')
As the Answer shows and you've discovered the problem isn't with the status of hold anyway; it was just a working hypothesis w/o having done the experiment to discover the behavior of axes before your subsequent example. In retrospect there was enough to diagnose I just didn't recognize it as, like you, I didn't suspect axes as being the culprit but was looking at more esoteric things like data values surprising you or somesuch.
Categories
Find more on Axis Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!