Novice_Developer
Novice_Developer

Reputation: 1492

Dynamically update the ylimits to fit data for horizontal-only zoom

Lets consider a simple example of a plot

y_axis = [randi([0,20],1,100)  randi([20,40],1,100) randi([0,20],1,100)];
x_axis = 1:300;
ax = subplot(1,1,1)
plot(x_axis,y_axis);
get(gca,'ylim')
zoom on
setAxesZoomMotion(zoom,ax,'horizontal');
axis tight 

enter image description here

Here the y-axis limit depends on the area I zoom in for example in the middle the minimum value of y is 20 but on the sides of the plot it is 0 , I have limited the zoom to the x axis only , now what i want is when i zoom in for example in the middle the y-axis limits resets itself to the minimum and maximum value of y axis currently visible. I have tried setting the axis to 'tight' and 'auto', is there any property or function which in MATLAB which does that ?

Upvotes: 0

Views: 159

Answers (1)

Suever
Suever

Reputation: 65470

You could use the zoom object and set the ActionPostCallback to a function which will call axis auto y to automatically re-calculate the ylimits to fit the visible data.

y_axis = [randi([0,20],1,100)  randi([20,40],1,100) randi([0,20],1,100)];
x_axis = 1:300;
ax = subplot(1,1,1)
hplot = plot(x_axis,y_axis);

hzoom = zoom(ax);
hzoom.Motion = 'horizontal';
hzoom.ActionPostCallback = @(fig,e)axis(e.Axes, 'auto y');

enter image description here

Upvotes: 2

Related Questions