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

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

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

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.
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)

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

Hi, thank you for your answer, it seems however I have been unclear in my formulation of my question. I have already made a separate dataset, that plots beautiful stars on the right places on my plot. The problem is that I loose all information on the plotwindow that has to do with the second axis, whenever I add to the first axis. I have tried using your formulation "plot(hAx(1),x,ystar,'r*')" in a form suiting my code, however, my problem persists.
I am not hellbent on using plotyy. I just want to have a plot with a series of data with some stars marking key datapoints on one axis, and a seperate plot on the other axis.
To help clarify my problem, I am looking at data obtained from some tuna. The tuna dives into deeper, colder, less oxygenated, water during the day, loosing body heat in the process. After some time, it returns to hotter/upper layers of water(s), either to reheat, or to fill up oxygen reserves. Noone knows for sure what drives it to go up, but it does. It then goes down, and the cycle repeats itself, often all day long. In my plotyy, I plot inner temperature of the fish in axis(1), and the depth on axis(2). The x axis is simply the #of the measurement (data were sampled every 1 minute), starting from 1 at 00:01, and ending at 1440 at midnight obviously. To this plot i now want to add data, consisting of rows extracted from the big huge datamatrix for the entire day. The datarows represent the measurements containing the 60 lowest measured values of bodytemperature for the Tuna. If the lowest temperatures are equally distributed between dives, it may point towards bodytemperature being the factor making the tuna swim up.
The extracted data contains the appropriate x-values, so no big fuzz is needed to make sure they go in the right x-positions. My problem is solely focused on keeping the data tied to axis(2) when adding the start. I can see they get plotted when the program is running, only to dissappear as soon as I add the stars. This does not happen in my simpler example code, and I don't get why. I suspect it to be the amount of data points, but then again, I don't know.
Again, thank you for your answer. Hoping this clarifies my exact problem, Nicki Eriksen
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 :) )
a) I don't know how you mean to do what you suggest. I believe the hold on goes out and holds all axes, as the documentation for "help hold" says:
"HOLD ON holds the current plot and all axis properties so that subsequent graphing commands add to the existing graph."
As to my exerpt of my actual code having hold on at the end; it is not really at the end, I havent added the code to plot the stars, as I did not get it to work. moving around hold on has not made my problem go away anyway.
b) Ylim is only applied to this one axis. The skale on axis(2) is preserved, as seen in this photo of a figure produced by my code:
I will look into some way of making code that duplicates my error.
Thanks again.
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.
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.

Asked:

on 20 Nov 2013

Commented:

dpb
on 21 Nov 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!