Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: When I zoom in a plotyy graph yTicks don't update automatically

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:

enter image description here

like image 644
TAK Avatar asked Jan 08 '23 04:01

TAK


2 Answers

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

enter image description here

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')
like image 168
marsei Avatar answered Jan 09 '23 18:01

marsei


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:

yay

Note that I've only linked the x, but you can link both x and y axes with the linkaxes call.

like image 43
sco1 Avatar answered Jan 09 '23 16:01

sco1