Clear Filters
Clear Filters

Adding to plotyy / Datarepresentation using multiple representations of same data.

1 view (last 30 days)
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

dpb
dpb on 20 Nov 2013
Edited: dpb on 20 Nov 2013
The problem is with axes as standalone and the plot order in plotyy.
If you execute
axes(TurnerPlot(2))
the RH axes data will then reappear. I don't have time at the moment to dig into precisely why this is occurring but the way to avoid the problem is to use the handle argument in calling the various routines instead of axes.
TurnerPlot=plotyy(Dataset(:,1),Dataset(:,5),Dataset(:,1),Dataset(:,3));
set(TurnerPlot(2),'YDir','reverse');
ylabel(TurnerPlot(2),'Dybde (m)')
ylabel(TurnerPlot(1),'Kernetemperatur (C)')
ylim(TurnerPlot(1),[10 30]);
title(TurnerPlotName)
BTW, I checked and the same behavior occurs clear back to R12 and I presume from that from the beginning. It is owing to the way in which the layers are rendered but I don't know the real why otomh and don't have time to dig at the moment. I hadn't ever noticed it because I've always used the above forms as being more concise as well as precise in ensuring which is current at the time...which makes inserting other code between or between calls to axes much simpler as the axes call and the subsequent modification can't get separated. It has the one drawback of requiring it every time instead of being able to just use the default current axis object as the target but imo that's a small price for the reliability. A USING...END syntax to tie stuff together or the like could be a "syntactic sugar" enhancement request.
  2 Comments
Nicki
Nicki on 21 Nov 2013
Your help, along with that from one of my fellow students, eventually solved my problem. There were multiple paths to my desired answer, but the problem I was having seems to have been in the loose axes calls. I can't say that I understand exactly how the layering of the plots work/are drawn, but I have a way to work around my problem. Thank you for taking your time to help me.
dpb
dpb on 21 Nov 2013
I'm not sure whether this behavior deserves a bug report or not -- you might just send the sample script to official Support and see what you get as a response. It could be even shorter for the purpose as the labels, etc., are of no consequence to the behavior.
Ideally axes would not have such side effects in this case but it may be that for other uses/needs the effect is critical to allow to hide a plane.

Sign in to comment.

More Answers (1)

dpb
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
Nicki on 20 Nov 2013
Ok, so I set out to replicate my error, and started by doing just my plotyy like this:
clc
close all
clear all
a=[[1:1440]'];
Dataset=[a a*0 (sin(a./20)+2)*100 a*0 cos(a./20)*9+20]
TurnerPlotName='PureRandomness'
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)')
axes(TurnerPlot(1));
ylim([10 30]);
title(TurnerPlotName)
hold on
And this gives me a plot with all the right axis, but no data displayed on axis(2). Is this just my computer hitting midlife crisis, or do you get the same? This makes no sense to me whatsoever.
dpb
dpb on 20 Nov 2013
Edited: dpb 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.

Sign in to comment.

Categories

Find more on Two y-axis 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!