As written above, I want to zoom into a graph that was created with plotyy.
When I do this, the yTicks don't update to the new limits that are visible.
So it can happen, if you zoom too much, that you don't see any yTicks.
I found the ActionPostCallback function, but I don't get it work, whilst the xTicks are fine.
Code:
figure, plotyy(1:10,1:2:20,1:10,1:0.5:5.5)
Results in:

You may want to set the YTickMode property to auto.
h = plotyy(1:10,1:2:20,1:10,1:0.5:5.5)
set(h, 'YTickMode','Auto')

Complete code:
figure
subplot(121)
h1 = plotyy(1:10,1:2:20,1:10,1:0.5:5.5)
title('Original')
subplot(122)
h2 = plotyy(1:10,1:2:20,1:10,1:0.5:5.5)
title('Zoom')
set(h2, 'YTickMode','Auto')
Because this behavior doesn't seem to be present in a 'normal' plot call, this looks like an internal bug with the creation of the axes objects by plotyy. As one alternative, you can stack multiple axes on top of each other and therefore utilize the 'default' (for lack of a better word) zoom behavior. This approach also allows you to fully control the behavior of both axes independently and avoid the seemingly many foibles of plotyy.
I've slightly adapted one of my previous answers as an example for this situation.
% Sample data
x = 1:10;
y1 = 1:2:20;
y2 = 1:0.5:5.5;
% Create axes & store handles
h.myfig = figure;
h.ax1 = axes('Parent', h.myfig, 'Box', 'off');
if ~verLessThan('MATLAB', '8.4')
% MATLAB R2014b and newer
h.ax2 = axes('Parent', h.myfig, 'Position', h.ax1.Position, 'Color', 'none', 'YAxisLocation', 'Right');
else
% MATLAB 2014a and older
ax1position = get(h.ax1, 'Position');
h.ax2 = axes('Parent', h.myfig, 'Position', ax1position, 'Color', 'none', 'YAxisLocation', 'Right');
end
% Preserve axes formatting
hold(h.ax1, 'on');
hold(h.ax2, 'on');
% Plot data
plot(h.ax1, x, y1, 'b');
plot(h.ax2, x, y2, 'g');
linkaxes([h.ax1, h.ax2], 'x');
And a sample image:

Note that I've only linked the x, but you can link both x and y axes with the linkaxes call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With