plotyy is not working properly???

6 views (last 30 days)
Chris E.
Chris E. on 28 Oct 2013
Commented: Chris E. on 3 Feb 2014
Well I have an application for "plotyy" function, So I find if I ever zoom in anywhere on the plot, one of the axis stick in a zoomed position when you click on "reset to original view". One axis resets but the other does not. I did no modifications to "plotyy" function, so I was wondering if someone out there knows how to fix this issue with "plotyy" or if someone knows why it is doing this. If someone needs code to work with, here is an example:
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
To see the problem, simply zoom in and then zoom out or reset with the right click of the mouse! Thanks!

Accepted Answer

David Sanchez
David Sanchez on 28 Oct 2013
Your code is right, I run it and it works for me with no problem. Besides, it is from matlab documentation, for which it should work properly. I think your problem lies on your machine/system.
  1 Comment
Chris E.
Chris E. on 28 Oct 2013
Well I am running on linux machine... I wonder if it is my OS or my machine... Thanks anyway....

Sign in to comment.

More Answers (2)

Bernard on 31 Jan 2014
I am on a Windows machine and it does the same thing for me.
Furthermore, if you were to plot several objects on each of the two axes, when you zoom in, it will not affect any of the line objects except the one that was plotted last. I cannot zoom in on the solid green line.
hold on
x = 0:0.01:20;
yL{1} = 1*sin(x);
yL{2} = 2*sin(x);
yR{1} = -5*sin(x);
yR{2} = -6*sin(x);
LS = {'-', '-.'}; %Line Style
for f = 1:2 %Two data files plotted on the same Y-Y plot
[ax, hL, hR] = plotyy(x,yL{f},x,yR{f});
'XLim', [0 5],...
'YLim', [-2 2])
'XLim', [0 5],...
'YLim', [-7 7],...
'XTick', [])
set(hL, 'LineStyle', LS{f})
set(hR, 'LineStyle', LS{f})

Bernard on 3 Feb 2014
I have found the solution:
fh = figure;
for f = 1:n
[AX(:, f), H1(f), H2(f)] = plotyy(x1{f}, y1{f}, x2{f}, y2{f}); %Plot several traces on the same YY-plot.
OldYLim1(f,:) = get(AX(1, f), 'YLim'); %Store the old YLim's for future reference
OldYLim2(f,:) = get(AX(2, f), 'YLim');
hZoom = zoom(fh); %Get the zoom mode object handle
hPan = pan(fh); %Get the pan mode object handle = AX; %Store the axis handles in a structure (so that a single variable can be passed to the function)
handles.OldYLim1 = OldYLim1; %Store the old y limits in the handles structure for the same reason
handles.OldYLim2 = OldYLim2;
set(hZoom, 'ActionPostCallback', {@zoomyy, handles}); %Set the Action Post Callback function for the Zoom and Pan features to a user-created function
set(hPan, 'ActionPostCallback', {@zoomyy, handles});
function zoomyy(fig,evd,handles) %#ok<INUSL>
ax =;
OldYLim1 = handles.OldYLim1;
OldYLim2 = handles.OldYLim2;
for f = 1:length(ax(2,:))
NewXLim1 = get(ax(1,f),'XLim');
NewYLim1 = get(ax(1,f),'YLim');
YFactor = OldYLim2(f,:)./OldYLim1(f,:);
NewXLim2 = NewXLim1;
NewYLim2 = NewYLim1.*YFactor;
set(ax(2,f), 'XLim', NewXLim2);
set(ax(2,f), 'YLim', NewYLim2);
  1 Comment
Chris E.
Chris E. on 3 Feb 2014
Thank you! Its been a while since I have needed that, but I will look through it and update it. Thanks!

Sign in to comment.


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!