File Exchange

image thumbnail

DRAGZOOM - Drag and zoom tool

version (18.9 KB) by Evgeny Pr
DRAGZOOM allows you to handy interactively manage the axes in figure.


Updated 05 Jun 2011

View Version History

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

Cite As

Evgeny Pr (2021). DRAGZOOM - Drag and zoom tool (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (94)

Snow Summer

What a wondfull tool! Actully I need a customized signal labeler, I think this tool can be modified as a good labeler which can be faster than MATLAB Signal Labeler APP.

alok pandey

This is really cool contribution. Highly recommended and appreciated!

marco taruselli

The best function I've never tried!


This is a fantastic kit, hope the new version will support the datetime axis. Otherwise, I can only try to write it myself.

Simon Parten

Couldn't get it to work on a stem plot with datetime x axis. Any hints?

Mohammad Askari

This tool is by far the best interactive 2D/3D figure control widget I have found in terms of speed and performance. It is nearly flawless. As long as your axes are direct children of the figure window, the zoom option works fine. But, I was personally developing a GUI and 3D zooming was not functioning correctly. So, in case your axes is placed in a container (other than the main figure handle), you may come across this issue. I made a quick fix to this problem and thought to share it here. Scroll to GetCursorCoordOnWindow function (line 2292) and add the following lines to the function:

if strcmp(hAx.Parent.Type,'figure')
containercrd = [0 0];
containerabspos = getpixelposition(hAx.Parent,true);
containercrd = containerabspos(1:2);

Also replace the following lines:
x = crd(1);
y = crd(2);

with this:

x = crd(1) - containercrd(1);
y = crd(2) - containercrd(2);

Here's an example to what I'm talking about. Run the following code before and after applying the fix:

hfig = figure('position',[100 100 1000 500]);
container1 = uipanel('parent',hfig,'position',[0 0 0.5 1]);
container2 = uipanel('parent',hfig,'position',[0.5 0.2 0.5 0.8]);
hax = axes('parent',container2);
x = -pi*2:0.1:pi*2;
y = sin(x);
n = 2^5-1;
[x,y,z] = sphere(n);

Mohammad Askari


Marco Furlan

Guojin Feng


Kangcheng Hou

Marko Usaj

Really good work!
Can anyone help me how to get/store the coordinates which are shown after you enable 'c' - On/Off Pointer Symbol 'fullcrosshair'? That would be awesome! Thanks for anyone help and time!

Marco Furlan

Excellent work!

Many thanks for sharing, it improves my work efficiency a lot

Gustav Potgieter

Thanks, great work! I have opened your "test_subplot_2D" file and it works perfectly. I was just wondering, can all three subplots be zoomed/panned etc. at the same time?
You have included: " 'l' - On XY / Off X,Y,XY linking 2-D axes (Synchronize manage) " under "2D and Image modes: " in this page, this might be the answer? But, my understanding to make use of it may be a bit off?

I see the creator of DRAGZOOM, Evgeny Pr, has not been active on his account for a year. Maybe someone else could help? Thanks.

Lorenzo Gasperi

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



Steven Sagona

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

Christine Sai-Halasz

Arthur Queiroz

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


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

@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).

Jonathan Lister

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

@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

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


Aurelien Queffurust

Really Excellent!

Evgeny Pr

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

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. :)

Matthew Arthington

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

fig = get(0,'PointerWindow');


fig = gcf;

Matthew Arthington

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

@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

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

@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


This is a great function!


Mohammed El-Said

Evgeny Pr


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

Nate Jensen

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?




Finally get it running! Great work!



Great work.


Great Job.

Thanks for sharing


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

Wolfgang Schwanghart

Excellent tool!

Evgeny Pr

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


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


Evgeny Pr

I'll check this possibility

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

Please, update DRAGZOOM to the latest version.


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.



"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.


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?


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

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


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.

Etienne O'Brien

It now works. Thank you.

Evgeny Pr

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

Etienne O'Brien

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

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

Etienne O'Brien

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

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.

Etienne O'Brien

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

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

karlo gonzales

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

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?

karlo gonzales

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

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

Evgeny Pr

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

Ben, thanks for your feedback.


Very nice function! Matlab should take this.


Mega cool! That works!! Thanks!!


Cool! Thanks!

Evgeny Pr

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


Thanks. hope you will find a solution:)

Evgeny Pr

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.


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...?

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!