File Exchange

image thumbnail

akZoom

version 1.10.1.0 (12.9 KB) by Alexander Kessel
Zooming and panning with the mouse in 2D plots. Supports plotyy, loglog, subplot, etc.

4 Downloads

Updated 14 Mar 2018

View License

allows direct zooming and panning with the mouse in 2D plots.
Default mouse button functions (can be customized, see below):
Scroll wheel: zoom in/out
Left Mouse Button: select an ROI rectangle to zoom in
Middle Mouse Button: pan view
Right Mouse Button: reset view to default view
SYNTAX:
akZoom
akZoom(___, handles, ___)
akZoom(___, mButtons_ZoomPanReset, ___)
akZoom(___, zoom_direction, ___)
Note: all input arguments are optional and can be given in arbitrary
order.

DESCRIPTION:
akZoom() activates mouse control for all axes-objects in the current
figure with default settings.

akZoom(___, handles, ___)
activates mouse control only for the axes given by the handles
argument which can be:
a) a single axis handle.
b) an array of axis handles. In this case all axes will be linked,
i.e. panning and zooming in one of the axes will affect the others as well.
c) a cell array consisting of axis handles and/or arrays of axis
handles. In this case mouse control is activated for all axes inside
the cell but linking is only activated for axes in a common array.
d) one of the following strings:
i) 'all': activates mouse control for all axes in all open figures.
The axes will not be linked except for axes belonging to the same
plotyy-plot or yyaxis-axes
ii) 'all_linked': activates mouse control for all axes in all open
figures and links all axes.

akZoom(___, mButtons_ZoomPanReset, ___)
will use akZoom with customized mouse buttons.
mButtons_ZoomPanReset can be:
'lmr', 'lrm', 'mlr', 'mrl', 'rlm' or 'rml'
where the letters stand for the left, middle and right mouse buttons
So 'lmr' means: left=ROI-zoom, middle=pan and right=reset.
Note: If you want to use a certain mouse button pattern as default,
just change mButtons_ZoomPanReset_default in the "Settings" section
below.

akZoom(___, zoom_direction, ___)
will use akZoom with zooming only along the specified direction.
zoom_direction can be: 'x', 'y', or 'xy'
Note: If you want to use a certain zoom direction as default, just
change zoom_direction_default in the "Settings" section below.

For further customization check the "Settings" section in the code.

EXAMPLES:
%% Simple Plot
figure
plot(10:24,rand(1,15));
akZoom();

%% Subplots (independent zooming for the axes)
figure
for k = 1:4
y = rand(1,15);
subplot(2, 2, k);
plot(y);
end
akZoom();

%% Subplots (mixture of linked and independent zomming for the axes)
figure
ax = NaN(4,1);
for k = 1:4
y = rand(1,15);
ax(k) = subplot(2, 2, k);
plot(y);
end
akZoom({[ax(1),ax(3)],ax(2),[ax(3),ax(4)]});

%% Different figures (linked)
figure;
imshow(imread('peppers.png'));
ax1 = gca;
figure
imshow(rgb2gray(imread('peppers.png')));
ax2 = gca;
akZoom([ax1,ax2]);

%% Zoom only along the x-axis
figure
plot(log(1:150),rand(1,150));
akZoom('x');

%% Find more examples in the file "akZoom_examples.m"

KNOWN BUGS:
- none at the moment

Author: Alexander Kessel
Affiliation: Max-Planck-Institut für Quantenoptik, Garching, München
Contact : alexander.kessel <at> mpq.mpg.de

CREDITS:
- Rody P.S. Oldenhuis for his mouse_figure function which served as the
template for akZoom
- Benoit Botton for reporting on an issue with axes nested in GUIs
- Anne-Sophie Girard-Guichon for reporting and fixing a bug occuring when
fast scrolling with mouse wheel
- useruser for the simplfication of the wrapper functions

Cite As

Alexander Kessel (2020). akZoom (https://www.mathworks.com/matlabcentral/fileexchange/41426-akzoom), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (22)

grega

Great function and great workaround to zoom in on "polar" plots. Any way to also make this work for "polarplot" plots?

rokP

Great func, thanks a lot!!!

@useruser: Thanks for the suggesed simplification. I updated the wrapper files accordingly.
Regarding your feature request: In my opinion the ROI solution chosen by MATLAB is not ideal as it does not allow for strong zooming in one dimension. I therefore implemented another solution in the current version that switches to unidirectional zooming once the ROI spreads over more than 95% (can be adapted if necessary) of the plot width/height.

@Peter MacGonagan: You can now constrain the zoom to one dimension by calling akZoom('y'). Note that if you like to autoscale the plot in the non-zoom direction according to the data in the plot, you can do so by setting the value of the autoscale_nonzoom_direction variable in the settings section to true.

Is it possible to constrain the zoom on y axis? My signal are limited from 0-100%. I would like that a kind of autoscale on y axis...

useruser

The wrappers can be simplified using builtin function. For example:

function h = plot(varargin)
% wrapper for the original plot function
h = builtin('plot', varargin{:});
akZoom();

The support for vertical/horizontal zoom (like in 2016b, if ROI is very close to a vertical or horizontal line respectively), would be nice.

@Anne-Sophie Girard-Guichon: Thanks for tracking and fixing this bug. I included your correction in the new submission.

@DGM: Thanks for your comment. I'm afraid there is not much I can do regarding the imshow-function: akZoom does actually fill the parent axis. The problem is that imshow() sets the parent axes to fit the image in the first place with the DataAspectRatio setting.
A workaround is to use image() instead and fix the aspect ratio by "axis equal". This way the original axes size is preserved. Here is a comparison:

----------------------------------------
img = imread('peppers.png');

figure
imshow(img);
set(gcf, 'Position', [500,600,400,150]);
set(gcf,'Name', 'imshow()', 'NumberTitle', 'off')
akZoom

figure
set(gcf, 'Position', [500,350,400,150]);
image(img);
set(gca, 'DataAspectRatio', [1 1 1])
set(gca, 'visible', 'off')
set(gcf,'Name', 'DataAspectRatio [1 1 1] (same as imshow)', 'NumberTitle', 'off')
akZoom

figure
set(gcf, 'Position', [500,100,400,150]);
image(img);
axis equal
set(gca, 'visible', 'off')
set(gcf,'Name', 'axis equal', 'NumberTitle', 'off')
akZoom

DGM

So far everything works correctly and quickly. This makes me wish it were just the default view control behavior. Perhaps the only thing missing in that regard would be functionality to force IMSHOW/IMAGE to fill its parent axes when zooming in.

Otherwise I think you just ended several hours worth of frustration for me.

Hi Alexander,

You did a very good job. Thanks for sharing it.

However, I had an issue when fast scrolling for a zoom in, it was actually zooming out !

My quick fix was (line 208 in akZoom.m):

zoomFactor = (abs(sc)*(1+wheel_zoomFactor/100))^sign(sc);

instead of

zoomFactor = abs(sc)*(1+wheel_zoomFactor/100)^sign(sc);

I added parentheses, otherwise abs(sc) is not taken into account if sign(sc) is negative, which was giving me a zoom factor > 1 (zoom out).

chen xinfeng

It's good and work nice. The demo is clear.

Hi Astrolix,

Thanks for the notification! I simply forgot to add plotyy_fixZoom to the zip-folder. Sorry for that.

The file, however, you found via Google is correct and should do the job (at least it does on my R2009b and R2013a).

I uploaded a version with included plotyy_fixZoom. Please report the error message if it still does not work for you.

As a workaround you can also comment out the call in my plotyy.m file. plotyy_fixZoom has nothing to do with the akZoom-functionality and just fixes a general Matlab-bug with plotyy tick-labels.

Astrolix

The latest version of this function/package does not work.

It is missing a file named plotyy_fixZoom(AX). I found one version of it through a google search that basically contained:

set(AX(2), 'XTickLabel','','XAxisLocation','Top')
set(AX(1),'Box','off');
set(AX(2),'Box','off');
set(AX(1),'YTickMode','auto');
set(AX(2),'YTickMode','auto');

I tried to put it in, but received an error based on the input argument, that plotyy (from this package). Next, I just pasted what plotyy_fixZoom does, and I received a "undefined function ..." on akZoom.

Hi Benoit,

Thanks for pointing this error out! I fixed it in my recent update.

Hi alexander,

this is a very good piece of code, so useful. thank you for sharing.

I ran into the following issue:
in a GUI I built, my axes are within a uipanel. so I kept getting this error:
" Error using hg.uipanel/set
The name 'WindowScrollWheelFcn' is not an accessible property for an instance of class 'uipanel'. "

my quick fix was to add the following lines at the end of the parse_handles function:

for nn = 1:length(h_fig)
while ~strcmpi( get( h_fig(nn), 'Type'), 'figure')
h_fig(nn) = get(h_fig(nn), 'Parent');
end
end

some quick testing shows it works

Hi Vincent,
Thanks for your idea. It should be feasible. I'll think about including it in the next release.

Vincent

Is it easy to add the feature with linked glasses to only keep x linking. So that you can pan/zoom in a subplot and only the x limits in the other linked subplots change?

arnold

great stuff!
Always wonder why Mathworks can't seem to manage to include such "basic" functionality, puzzling.

Shahab

Hi Francesco,
Thanks for the feedback. I just uploaded the next version which will be published as soon as the MathWorks team has finished reviewing it.

Regarding the zoom/pan bug: Yes I have the same problem and on the way for a solution I found that many people have similar issues of loosing window focus after some operation. Sometimes they could find a workaround for this Matlab behaviour but for the case where you delete a patch object (like in akZoom) I couldn't find a solution. However, I wanted to get rid of the patch object anyway, so I replaced it with a rectangle: voilà!

Regarding customizing the mapping of the mouse buttons: you can do this now easily in the "Settings" section of the akZoom function. I guess everyone has a certain scheme he/she wants to work with but there is no need to change this really at runtime. Correct me if I'm wrong.

Thanks for this useful submission!
Just one problem I've found so far on my MATLAB r2013a: after a zoom by left-click (zoom by creating a rectangle), I cannot used anymore the scroll wheel to zoom in or out... do you have the same issue?

Also it would be very nice to allow the caller to specify the mapping of the mouse buttons: e.g., I would like to pan with the left mouse button and to zoom by a rectangle with the middle button... I can modify that in the code myself, but having the function configurable at runtime would be nice IMHO :)

Hi Felix,

Thanks for your comment and rating!

What you could do is to create a wrapper for the MATLAB-plot function:

function h = myPlot(varargin)
h = plot(varargin{:});
akZoom(get(h,'Parent'));
end

Save this function in a folder of your choice and add it to the Matlab-Search-Path.

Now you can use the function myPlot the same way you would use "plot" and akZoom is included automatically.

Felix

thanks, works very well!
I wonder, is there a way to automatically have it run for every plot, without typing akZoom every time...

Updates

1.10.1.0

updated file

1.10.0.0

* constraining the zoom to only the x- or y-axis now supported
* in xy-zoom mode the user can constrain the zoom to one axis by drawing an ROI rectangle that spreads over more than 95% of the plot height or width
* the wrappers are simplified

1.9.0.0

fix a bug that occurs when fast scrolling with mouse wheel (thanks to Anne-Sophie Girard-Guichon)

1.8.1.0

included missing file: plotyy_fixZoom.m

1.8.0.0

* solved problems with axes nested in GUIs (thanks to Benoit Botton)
* solved problems with out-of bounds logarithmic-plots
* improved error handling in wrapper functions
* bugfix for fast scrolling (could previously result in error messages )

1.7.0.0

*supports now loglog & semilog
*mouse buttons customizable
*unsensitive to data markers & legends
*checks for valid axis limits (log!)
*automatically callable for 2D plots

1.6.0.0

updated Description

1.5.0.0

-zooming: replaced patch by rectangle -> faster, visually not dependent on renderer, bug fixed that prevented scroll wheel events after zoom
-mapping of mouse buttons can be customized
-easy usage with 'all' and 'all_linked' (works now with plotyy)

1.3.0.0

*bugfixes
*out-of-the-box functionality with subplots and (multiple) plotyy-axes
*arbitraty linking between different axes can be easily set up

MATLAB Release Compatibility
Created with R2016b
Compatible with any release
Platform Compatibility
Windows macOS Linux