Data "hides" when using plotyy

4 views (last 30 days)
Jason
Jason on 13 Feb 2015
Edited: dpb on 19 Feb 2015
I am using the following to plot two data sets ont he same graph
[AX,H1,H2] = plotyy(A,B,A,C,'plot');
It is in a loop. The first time I run this everything is fine, but if I run again, the second data set disappears after plotting the last point. It is visible upto this time.
set(H2,'Visible','on'); %# Make 2nd y axis visible, H2 as sometimes is disappears
I have used, but to no effect.
  6 Comments
dpb
dpb on 13 Feb 2015
Well, as the other poster notes, w/o any data we can't reproduce your efforts to 'spearmint w/o trying to build an example from scratch. As he, I'd suggest building a runnable small demo that illustrates the problem that can paste/post. Doing that may, in fact, lead to discovering the difficulty on your own in which case you can post the solution! :)
Jason
Jason on 13 Feb 2015
It appears not to be due to any of the code in the loop above, so Im checking my code for after the loop. I have noticed, that as I want to set the yscale for the left y axis myself using code below, its not doing it:
set(AX(1),'YTick',newYTicks,'YTickLabel',num2str(newYTicks'))
where
newYTicks =
0 50 100 150

Sign in to comment.

Accepted Answer

dpb
dpb on 18 Feb 2015
Edited: dpb on 18 Feb 2015
OK, I redid your callback function as follows for the LH plot. It produces same result for multiple clicks now altho needs a reset function to clear the data going forward and I also didn't worry about rescaling the dummy data to be within the axes ranges and all...
Take all the one-time stuff out of the loop and instead of making more line handles with the repetitive calling of plotyy simply update the data arrays and refresh. That's much faster and also far less overhead plus you were overwriting line handles with every call with the same variable when calling inside the loop.
See the documentation in the Graphics section on Animation for more details on how the guts works.
You also need a cleanup callback for the GUI; when you click the Close window X this currently errors as stuff is deleted in wrong order--I "know nuthink!" about GUIs so don't know precisely how that's supposed to be handled. I presume GUIDE will lead you through the process.
But, this should show enough of how to address the various handle graphics objects to stop the conflicting handles problems you're currently having. NB: as I said initially, the top-level outer axes handle is never referenced directly after the initial plotyy call that creates the two axes handles and in this technique it's the two line handles that are referenced instead of axes to update the data.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
lc=30;
sum=0;
%axes(handles.axes1)
i=1;
y1=2*i;
y2=3*i;
[hAx,H1,H2] = plotyy(handles.axes1,i,y1,i,y2);
ylabel(hAx(1),'FM', 'FontSize',8,'Color','red');
ylabel(hAx(2),'Mean','FontSize',8,'Color','blue');
xlabel(hAx(1),'Tile Number','FontSize',8)
set(hAx,'XLim',[0 lc],'FontSize',8);
set(hAx(2),'Xtick',[]);
set(hAx(1),'YColor','r')
set(hAx(2),'YColor','b')
grid on;
set(H1,'LineStyle','--', 'color', 'r','Marker','*','MarkerSize',4);
set(H2,'LineStyle','--', 'color', 'b','Marker','.');
hold(hAx(1),'on'), hold(hAx(2),'on')
xdat=i;
for i=2:lc
%Generate some dummy data
i;
xdat=[xdat; i];
y1=[y1;2*i];
y2=[y2;3*i];
set(H1,'xdata',xdat,'ydata',y1)
set(H2,'xdata',xdat,'ydata',y2)
drawnow
%Plot sum of i on axes2
sum=sum+i;
%axes(handles.axes2)
%hold on;
%plot(i,sum)
%hold off;
end
%h=handles.axes1;
%hold on;
h_legend=legend(hAx(1),'FM','Mean');
set(h_legend,'FontSize',7);
%drawnow;
hL=plot(hAx(1),[20 20], [0 150],'color','k','linewidth',2);
  1 Comment
dpb
dpb on 18 Feb 2015
"Take all the one-time stuff out of the loop ..."
If your callback isn't actually a loop but are updating data from some source, put all that static stuff in some initialization function not in the callback to be executed every time. Or, at a minimum, have a "first call" flag to only execute the startup code once and branch around it for subsequent calls.

Sign in to comment.

More Answers (4)

Jason
Jason on 13 Feb 2015
OK, I've worked out what causes the problem, but don't know why it does. Once the loop above has finished, I then want to superimpose a line on this graph so I use hold on.
axes(handles.axes4);
hold on;
for i=0:numregions-1
plot([20 20],[0 Y1(2)],'k', 'LineWidth',1); %Y1(2) is Y1Max
end
hold off;
So adding the line whilst using hold on causes the 2nd y axis plot to hide??
  11 Comments
Jason
Jason on 17 Feb 2015
The only option i have is to save as a fig? do you mean zip it up? Thanks Jason
dpb
dpb on 17 Feb 2015
Edited: dpb on 17 Feb 2015
No, when you use the File menu SaveAs, save local copy then use the paper clip to attach that file. Then I should be able to copy the file itself instead of just open the figure (which has no menu options so can't save it locally and I'm not adept-enough to try to duplicate it that will match the script w/o more effort than care to try...
ADDENDUM
Or, actually, since you must have it saved on disk already to run the test function, use the Attach File paper clip and attach that existing file.
I tried saveas here from the command window with the figure displayed after opening w/ R2012b but that was unsuccessful...the resulting open ends up w/ nothing but an empty figure for some reason. Having never played w/ the GUI stuff I don't know what the deal is...

Sign in to comment.


Jason
Jason on 18 Feb 2015
Hi, thats exactly how i attached the above files test.fig and test.m using the attach paper clip. I will try again Thanks
  1 Comment
dpb
dpb on 18 Feb 2015
Edited: dpb on 18 Feb 2015
Hmmm....it wouldn't let me download the file previously; I'll try again, too. I thought it had been attached as an image the way it behaved.
[scuffle, drag, ...] OK, this time I got a download option and did work.
What should happen? If I then click the button I get an axes w/ only a single red dot with blue dot moving across. Subsequent times the axes blinks which I presume isn't supposed to be?
It appears the data you're generating is off the axes y-limits for the red trace after the first point? Is that intentional or an accident?

Sign in to comment.


Jason
Jason on 18 Feb 2015
No it should plot two data sets on the axes1 and another one on axes2. In reality i loop thru a setof images and plot their mean and std dev on axes1, but i didnt want to upload all the images.

Jason
Jason on 19 Feb 2015
Edited: Jason on 19 Feb 2015
Thanks for this. It seems tho as if the Y-axis is remaining fixed so the data is off the scale.
I have tried using the following to scale the Y1 axis and set the ticks: (AX is the handle I use not hAx)
Y1=get(AX(1),'YLim');
Y1(2); %Max value
Y1(1); %Min value
diff=Y1(2)-Y1(1);
Depending on the size of diff, I have different tick marks:
newYTicks = 0:10000:floor(max(get(AX(1),'YTick'))); %default
if (diff<40000)
newYTicks = 0:5000:floor(max(get(AX(1),'YTick')));
end
if (diff<10000)
newYTicks = 0:1000:floor(max(get(AX(1),'YTick')));
end
if (diff<1000)
newYTicks = 0:100:floor(max(get(AX(1),'YTick')));
end
if (diff<100)
newYTicks = 0:10:floor(max(get(AX(1),'YTick')));
end
And then set the axis ticks:
set(AX(1),'YTick',newYTicks,'YTickLabel',num2str(newYTicks')) %Use
set(AX(1),'FontSize',8)
drawnow;
But it seems my newTicks is incorrect:
ans =
171.8599
ans =
127.8709
diff =
43.9890
newYTicks =
0
If I do:
get(AX(1),'YTick')
I get a weird result: ans =
-1.0000 -0.5000 0 0.5000 1.0000
  6 Comments
Jason
Jason on 19 Feb 2015
OK thats very kind of you, please don't this ahead of other work, you have helped a lot already.
dpb
dpb on 19 Feb 2015
Don't worry on that score... :) I'm "retired" from the actual paid consulting gig so commitments are various community things or, when it's time actual farm work as returned to family farm some 15-yr ago, now...

Sign in to comment.

Categories

Find more on Two y-axis in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!