File Exchange

image thumbnail

DRAGZOOM - Drag and zoom tool

version 1.13 (18.9 KB) by

DRAGZOOM allows you to handy interactively manage the axes in figure.

39 Ratings



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

DRAGZOOM allows you to interactively manage the axes in figure. This simple function for usable dragging and zooming of axes, rotate 3D axes, using the mouse and the keyboard shortcuts.

- Supports 2D-Plots, 3D-plots, Log plots and Images
- Supports multiple mixed (subplots) (2D, 3D) axes on one figure
- Supports X,Y,XY linking of several 2-D axes (Synchronize manage)
- Rubber Band Region Zooming
- Magnifier Tool
- Handy Cross-hair Cursor
- Handy Zooming and Draging Plots
- Handy and Fast Rotate 3D Plots
- Control Enabled Status

dragzoom(hAxes) % array of axes handles
dragzoom(hFig, status) % status - set enable status 'on' or 'off'

Control Enabled Status of DRAGZOOM:
dragzoom(hFig, 'on') % Enable DRAGZOOM for figure "hFig"
dragzoom(hFig, 'off') % Disable DRAGZOOM for figure "hFig"
dragzoom('on') % Enable DRAGZOOM for figure "GCF"
dragzoom('off') % Disable DRAGZOOM for figure "GCF"

Interactive mode:
Available the following actions:

2D and Image modes:
- Mouse actions in 2D mode:
-- Normal mode:
"single-click and holding LB" - Activation Drag mode
"single-click and holding RB" - Activation Rubber Band for region zooming
"single-click MB" - Activation 'Extend' Zoom mode
"scroll wheel MB" - Activation Zoom mode
"double-click LB, RB, MB" - Reset to Original View

-- Magnifier mode:
"single-click LB" - Not Used
"single-click RB" - Not Used
"single-click MB" - Reset Magnifier to Original View
"scroll wheel MB" - Change Magnifier Zoom
"double-click LB" - Increase Magnifier Size
"double-click RB" - Decrease Magnifier Size

- Hotkeys in 2D mode:
'+' - Zoom plus
'-' - Zoom minus
'0' - Set default axes (reset to original view)
'uparrow' - Up or down (inrerse) drag
'downarrow' - Down or up (inverse) drag
'leftarrow' - Left or right (inverse) drag
'rightarrow' - Right or left (inverse) drag
'c' - On/Off Pointer Symbol 'fullcrosshair'
'g' - On/Off axes grid (only plots)
's' - On/Off smoothing plots
'x' - If pressed and holding, zoom and drag works only for X axis
'y' - If pressed and holding, zoom and drag works only for Y axis
'm' - If pressed and holding, Magnifier mode on
'l' - On XY / Off X,Y,XY linking 2-D axes (Synchronize manage)
'control+l' - On X linking 2-D axes
'alt+l' - On Y linking 2-D axes

* 3D Mode:
- Mouse actions in 3D mode:
"single-click and holding LB" - Activation Drag mode
"single-click and holding MB" - Activation 'Extend' Zoom mode
"single-click and holding RB" - Activation Rotate mode
"scroll whell MB" - Activation Zoom mode
"double-click LB, RB, MB" - Reset to Original View

- Hotkeys in 3D mode:
'+' - Zoom plus
'-' - Zoom minus
'0' - Set default axes (reset to original view)
'uparrow' - Rotate up-down
'downarrow' - Rotate down-up
'leftarrow' - Rotate left-right
'rightarrow' - Rotate right-left
'ctrl'+'uparrow' - Up or down (inrerse) drag
'ctrl'+'downarrow' - Down or up (inverse) drag
'ctrl'+'leftarrow' - Left or right (inverse) drag
'ctrl'+'rightarrow' - Right or left (inverse) drag
'1' - Go to X-Y view
'2' - Go to X-Z view
'3' - Go to Y-Z view
'v' - On/Off Visible Axes
'f' - On/Off Fixed Aspect Ratio
'g' - On/Off Visible Grid


% 2D
x = -pi:0.1:pi;
y = sin(x);
figure; plot(x, y);

% Image
I = imread('cameraman.tif');
figure; imshow(I, []);

% 3D
[X,Y] = meshgrid(-3:.125:3);
Z = peaks(X,Y);
axis([-3 3 -3 3 -10 10]);

x = -pi*2:0.1:pi*2;
y1 = sin(x);
y2 = cos(x);
hax1 = subplot(2,1,1); plot(x,y1, '.-r')
hax2 = subplot(2,1,2); plot(x, y2, 'o-b')
dragzoom(hax1); % manage only axes 1

Comments and Ratings (79)

It woul be fantastic if it would work with yyplot!
Error using
While setting the 'YLim' property of 'Axes':
Value must be a 1x2 vector of numeric type in which the second element is larger than the first and may be Inf



Is there a way that I can zoom in just with respect to the x or y axis? (that is, keep the x axis the same and zoom in on the y-axis)

Also, if I graph two figures in the same plot, is there a way I can independently zoom in-and-out each plot? Also is there a way I can zoom on each of them together? (and can I incorporate these commands when zooming in to the previously asked-for ability to zoom in with only the x-axis?)

Joe Passman


This is a fantastic tool. Does it have any functionality to save the current figure after the manipulations are done? I basically want to do this because I want to further post-process an image after manipulating with your tool.


Joe Passman


phyatt (view profile)

This is an excellent tool. I made the zoom/pan/position persistent and make a gist showing how to do it.

Keep up the great work!

Evan Luo

This tool is great!! I was using this tool to trace the coordinate on my 2D plot created from GUI. The 'c' - On/Off Pointer Symbol 'fullcrosshair' works great without showing my legend. Whenever I show my legend, the mouse icon disappear as well as the cross pin. How would I fix that?

Johannes Korsawe

maybe i was wrong.
Somehow my figure, where i wanted to use dragzoom, had a saved property "UserData" which referred to an 1x0 empty Graphics array (?wherever this came from?). As you also use this property, i just had to empty UserData (by replacing the above value with []) to make dragzoom work.

It seems, that 2014b still respects a lot of the old notation.

Congrats to this great submission!

Evgeny Pr

Evgeny Pr (view profile)

@Johannes Korsawe

Since R2014b the graphics system has been rewritten via object-oriented approach. The "dragzoom" function must be fully rewritten for support R2014b.

I do not know when I can do it. I do not have access to Matlab R2014b now.

Forks welcome. :)

Johannes Korsawe

Does not work with Matlab R2014b. Any plans to migrate?


Very nice submission! Is there an easy way to add the ability to make the magnifier sticky (i.e. it doesn't disappear when you let go of the "m" key)? And the follow-on question would be could you then have a second magnifier box with the next "m" press? I'm thinking something along the lines of shift+m to make a new sticky magnifier and then ctrl+m to delete all magnifiers (or something like that).

I really like this submission not just for all the handy interactivity it provides, but also for the great handling of the windowfcn callbacks. Seeing how you handled that was worth the download. I did notice one small bug. When you call dragzoom on an axes that is actually a child of a panel using the scroll button doesn't work. Other than that, great submission.


Thanks Evgeny, this is a really useful tool. Although I downloaded it a while ago I have only recently started to use it regularly.

It feels like a more intuitive way interacting with a plot, dragging and zooming like in Google Maps. This is much more natural (and quicker) for me than clicking the pan and zoom tools. I think this facility should be built into MATLAB graphics (which are badly in need of a revamp).

Changshun Deng

I like this tool! Great job!

Evgeny Pr

Evgeny Pr (view profile)

@Terence Ryan,
GINPUT function takes away control on itself (executes its callbacks for interact events). I think, will be problematic to achieve native GINPUT and DRAGZOOM collaboration. :(

Evgeny Pr

Evgeny Pr (view profile)

@Ippei Kotera,
Unfortunately, the simple solution to this problem I don't know. This problem appears in case of set property "String" for graphics object "text".
For example (in dragzoom PointerCrossUpdate func):
set(mPointerCross.htext(left), 'String', yValStr)

Terence Ryan

This is a great function and it works amazingly. It is almost perfectly what I need. Maybe someone can help with my problem... I would like to use this function to zoom and drag on my plots, but also use ginput to select points on my plot. Is this possible?


Ippei Kotera

Very nice tool. One small thing I noticed is when I use OpenGL renderer, as in the following example, the crosshair disappears. Do you think you can find a way around? I'm using 2010a/Windows 7 64 bit.

set(h, 'Renderer', 'OpenGL');


Really Excellent!

Evgeny Pr

Evgeny Pr (view profile)

Hi Giacomo,

Yes, you can do this. I tried to do so, but something didn't work properly, so I retracted this decision in favour of that is done in matlab-function ZOOM.


really good function...
would it be easy to modify it to have 3D drag of the axes limits instead of moving the axes object in the figure?
(like the 2D case)
Thank you

Evgeny Pr

Evgeny Pr (view profile)

Hi Matthew Arthington,

This is a very good idea, but unfortunately there is dependence on the external function. But overobj2 is small function, it can be added inside dragzoom. :)

P.S. To make it work I also had to change line 22 of overobj2 from

fig = get(0,'PointerWindow');


fig = gcf;

Hi Evgeny,

This is a great function, thank you.

I've added a modification to mine so that it doesn't change the figure name, but instead it changes the mouse cursor when it hovers over the axes. I added:

hAxesTemp = overobj2('type','axes');
if ~isempty(hAxesTemp)

to the end of the WindowButtonMotionCallback2D function and I'm calling overobj2 from

Dave Ls

Evgeny Pr

Evgeny Pr (view profile)

@Dave Ls,

I am glad that all works! Thanks for a use my function.

Dave Ls

It is my mistake. I used "save link as ..." to save the file.

It is working now. Thanks a lot for the function.

Evgeny Pr

Evgeny Pr (view profile)

Please, specify the number of line.

Dave Ls

Hi Evgeny,

Not HotKey, I got error message when I run the file. It said "unexpected matlab operator".

Thanks a lot for your reply.

Evgeny Pr

Evgeny Pr (view profile)

@Dave Ls,

I not so well understand your problem. Hot keys don't work?

Dave Ls

This is the function I am looking for. But somehow I can not make it work. It doesn't recognize letters < and " in HTML, { in for loop. I am using R2009a. What is wrong? Please help. Thanks


Ben (view profile)

This is a great function!


Piotr (view profile)

Evgeny Pr

Evgeny Pr (view profile)


Thank You! I understand that you want to do. It is necessary to think how to do this in DRAGZOOM.

Nate Jensen

Nate Jensen (view profile)

Evgeny Pr,

First off, I love this function, great job! I am wondering though how to incorporate this with other callbacks that are normally assigned to the pan and zoom action post and pre callbacks?

I am plotting a lot of data and Matlab, or my computer, has trouble plotting all of the data and panning around or zooming in smoothly. So what I am doing is plotting a subset of the data and after I pan or zoom, I call the 'ActionPostCallback' to replot the data.

Do you have any idea how I could incorporate the 'ActionPostCallback' with your function dragzoom?




Martin (view profile)

Finally get it running! Great work!



Sherif (view profile)

Great work.


Great Job.

Thanks for sharing


Milos (view profile)

I think that the most universal number format would be MATLAB's shortG.

Excellent tool!

Evgeny Pr

Evgeny Pr (view profile)

Thank You!
Not yet, but I think I like to make a universal format for numbers.
Interested in any suggestions! :)


Milos (view profile)

This is great! Nice job, Evgeny. Is there a way to control number format when using cross-hair?


Milos (view profile)

Evgeny Pr

Evgeny Pr (view profile)

I'll check this possibility

@Jiro Doke,
Yes, in latest version I fixed of "Statistics Toolbox" dependency.

Please, update DRAGZOOM to the latest version.

Jiro Doke

Jiro Doke (view profile)

Oh, it seems like your most recent update (8 Dec 2010) fixed this toolbox dependency. You created a function "range" inside.

Try downloading the latest version.

Jiro Doke

Jiro Doke (view profile)


"range" is a function from Statistics Toolbox, so you need that toolbox to run it. You could replace all occurrences of "range" with your own implementation. Range is simply max(val) - min(val).


You should check "Statistics Toolbox" as a required toolbox for this submission.


Martin (view profile)

Hi, I have some problems getting this running. I have used the 2D example just as written in the description and then pressed the "+" key. What I get is following

??? Undefined function or method 'range' for input arguments of type 'double'.
Error in ==> dragzoom>GetCurrentZoomAxesPercent at 1196
        curentZoomX = range(defaultXLim) * 100 / range(xLim);
Error in ==> dragzoom>UpdateCurrentZoomAxes at 1168
        [curentZoomX, curentZoomY] = GetCurrentZoomAxesPercent(xLim, yLim);
Error in ==> dragzoom>ZoomKeys at 1048
Error in ==> dragzoom>WindowKeyPressCallback2D at 487
 ??? Error while evaluating figure WindowKeyPressFcn

I'm using Matlab 2010a. Must be a mistake by myside because all other have no such problems! Do you know whats wrong?


Ion (view profile)

Nice job, Evgeny Pr !
For apply the zooms and grab to more than one subplot at the same time, have you tried Linkaxes(handles) ?? It should work

Yuri K

Yuri K (view profile)

Super! Should be build into MATLAB. I'd vote. It would be nice to have a little mode-sensitive help window opening by a key (h or ?, for example).


Dmitry (view profile)

This is wonderful tool! Thanks!!! However it 3D mode I have probelms in activation of Rotate mode (I press and hold right mouse button but I select only region of zoom). I have also problems with hotkeys, f.e. '1' '2' and '3' does not change view as it is explained above.

It now works. Thank you.

Evgeny Pr

Evgeny Pr (view profile)

Etienne O'Brien,
This strangely...
Try commenting out the function CreateFigure (lines 683-691) and commenting line 312.

Yes, I think I am editing the file which runs. When I type 'which('dragzoom') this is what I get:


This is where all my working m files are kept.

Evgeny Pr

Evgeny Pr (view profile)

Etienne O'Brien, You are sure you are editing the same file that runs?
Command which('dragzoom') show full path to file, which runs.

Thanks Evgerny,

Unfortunately, this solution did not work.
a. I replaced "~" with several different letters of the alphabet but this messages was repeated.

E.g. function CreateFigure(src, a)
>> dragzoom
??? Error: File: dragzoom.m Line: 683 Column: 32
Unexpected MATLAB operator.

b. I then deleted this operator and the following happened:

function CreateFigure(src)
>> dragzoom
??? Error: File: dragzoom.m Line: 683 Column: 32
Unexpected MATLAB operator.

Evgeny Pr

Evgeny Pr (view profile)

Etienne O'Brien, you have an older version of MATLAB, that does not support operator "~" in function arguments. Replace "~" operator in line 683 at any other letter.

Hi Evgeny,
I determined to make your code work but I'm stuck! I'm a novice MATLAB user so my problem may be simple. I downloaded and saved your file to the same folder where my other m.files are. I created a Figure (a simple x, y plot) and entered 'dragzoom'. The following is what happened.

>> dragzoom;
??? Error: File: dragzoom.m Line: 683 Column: 32
Unexpected MATLAB operator.

Could you advise please?

Evgeny Pr

Evgeny Pr (view profile)

karlo gonzales, at this time this is not supported. I'll think about how to do it to be convenient for use.

Thanks Evgeny Pr, for reply.
I don't want to use it for a complex case, for example;
consider two subplots that contain a function and its derivative
subplot(2,1,1) plot (f(x,t))
subplot(2,1,2) plot (df(x,t)/dt)
So, for this 2-D graphs, can we apply the dragzoom function at the same time for both subplots?

Evgeny Pr

Evgeny Pr (view profile)

Thanks for your feedback, karlo gonzales!

If the axes will contain different plots (3D or 2D), different limits, etc, how to manage them at the same time in this case?

Wonderful job, Evgeny Pr!

Is there anyway to use this function, simultanously for subplots?
For example, change axes, zoom in/out.., happens at the same time for both subplot(2,1,1) and subplot(2,1,2).

Thansk again



Very nice work.
It would also be nice to disable it

Evgeny Pr

Evgeny Pr (view profile)

I'm currently doing support logarithmic plots. I completely forgot about it. It's unpleasant when something doesn't work as expected. :(

Evgeny Pr

Evgeny Pr (view profile)

Doug Harriman, Thanks for your feedback.
This possibility has been before, but I removed it. This should work as in the ZOOM function: "zoom on", "zoom off"?

Doug Harriman

Excellent work. Is it possible to disable this mode for an axes object once it has been enabled?


Evgeny Pr

Evgeny Pr (view profile)

Ben, thanks for your feedback.


Ben (view profile)

Very nice function! Matlab should take this.


Sophie (view profile)

Mega cool! That works!! Thanks!!


Sophie (view profile)

Cool! Thanks!

Evgeny Pr

Evgeny Pr (view profile)

Sophie, I've fixed the problem and submit files on Fileexchange. Should soon be updated.


Sophie (view profile)

Thanks. hope you will find a solution:)

Evgeny Pr

Evgeny Pr (view profile)

Thanks, Sophie!

This should work on this scheme:
hax1 = subplot(...);
hax2 = subplot(...);
hax3 = axes();
dragzoom(hax1); % for manage only Axes 1

But for some reason doesn't work. Always selects the last axes.
I'll fix this bug soon.


Sophie (view profile)

This is a great job. However, would it be possible to set this dragzoom option only on a given axis of the figure? Because if I have several axes, I would like this dragzoom option to work only if my mouse is over the image in this axis precisely...?



- add separate X, Y and XY linking 2-D axes


-fixed bug of magnifier
-fixed bug of legend capture and delete
-added more flexible format numbers of cross-hair cursor


- added linking 2-D axes (Synchronize manage)
- added info to figure title
- fixed bug of rubber band
- fixed bug draging rectangular images
- fixed and improved of magnifier tool (now always square size and independent from axes size)


- added control enable status of DRAGZOOM
- fixed bugs in cross pointer
- fixed bugs of set initial axes positions


- fixed problem with images (in last update was confused drag on the Y-axis)


- added support semilogx, semilogy, loglog plots


- fixed error using incorrect handle in saved figures.
- added option "smooth lines" for 2D plots


- Fixed a bug with the work on the saved figure


- Fixed bug with the specified axis (call: dragzoom(hAxes))
- If the mouse is outside of the axes, some interactive mouse actions are disabled

MATLAB Release
MATLAB 7.7 (R2008b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video