Data "hides" when using plotyy
4 views (last 30 days)
Show older comments
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
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! :)
Accepted Answer
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
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.
More Answers (4)
Jason
on 13 Feb 2015
11 Comments
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...
Jason
on 18 Feb 2015
1 Comment
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?
See Also
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!