Code covered by the BSD License  

Highlights from
DRAGZOOM - Drag and zoom tool


5.0 | 28 ratings Rate this file 56 Downloads (last 30 days) File Size: 18.9 KB File ID: #29276
image thumbnail

DRAGZOOM - Drag and zoom tool



04 Nov 2010 (Updated )

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

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

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


This file inspired Cluster Generator, Stackobj, and Dynamic Low, High And Band Pass Filter.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (65)
07 Jul 2013 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.

12 Jun 2013 Julian

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

06 Sep 2012 Changshun Deng

I like this tool! Great job!

05 Apr 2012 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. :(

05 Apr 2012 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)

05 Apr 2012 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?


15 Feb 2012 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');


15 Dec 2011 Aurelien Queffurust

Really Excellent!

04 Dec 2011 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.

02 Dec 2011 Giacomo

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

25 Nov 2011 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. :)

25 Nov 2011 Matthew Arthington

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

fig = get(0,'PointerWindow');


fig = gcf;

25 Nov 2011 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

24 Aug 2011 Dave Ls  
24 Aug 2011 Evgeny Pr

@Dave Ls,

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

24 Aug 2011 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.

24 Aug 2011 Evgeny Pr

Please, specify the number of line.

24 Aug 2011 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.

24 Aug 2011 Evgeny Pr

@Dave Ls,

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

24 Aug 2011 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

10 Aug 2011 Ben

This is a great function!

09 Aug 2011 Piotr  
09 Jul 2011 Mohammed El-Said  
03 Jul 2011 Evgeny Pr


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

02 Jul 2011 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?



14 Jun 2011 Martin

Finally get it running! Great work!

13 Jun 2011 Joachim  
07 Jun 2011 Sherif

Great work.

24 Dec 2010 Riccardo

Great Job.

Thanks for sharing

13 Dec 2010 Milos

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

10 Dec 2010 Wolfgang Schwanghart

Excellent tool!

09 Dec 2010 Evgeny Pr

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

09 Dec 2010 Milos

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

09 Dec 2010 Milos  
09 Dec 2010 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.

09 Dec 2010 Jiro Doke

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.

09 Dec 2010 Jiro Doke


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

09 Dec 2010 Martin

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?

09 Dec 2010 Ion

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

06 Dec 2010 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).

29 Nov 2010 Dmitry

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.

28 Nov 2010 Etienne O'Brien

It now works. Thank you.

28 Nov 2010 Evgeny Pr

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

27 Nov 2010 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.

27 Nov 2010 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.

27 Nov 2010 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.

27 Nov 2010 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.

26 Nov 2010 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?

25 Nov 2010 Evgeny Pr

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

25 Nov 2010 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?

25 Nov 2010 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?

25 Nov 2010 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

21 Nov 2010 Laurent

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

20 Nov 2010 Evgeny Pr

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

19 Nov 2010 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"?

19 Nov 2010 Doug Harriman

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

19 Nov 2010 Laurent  
13 Nov 2010 Evgeny Pr

Ben, thanks for your feedback.

12 Nov 2010 Ben

Very nice function! Matlab should take this.

11 Nov 2010 Sophie

Mega cool! That works!! Thanks!!

11 Nov 2010 Sophie

Cool! Thanks!

11 Nov 2010 Evgeny Pr

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

11 Nov 2010 Sophie

Thanks. hope you will find a solution:)

10 Nov 2010 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.

10 Nov 2010 Sophie

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

10 Nov 2010

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

13 Nov 2010

- Fixed a bug with the work on the saved figure

15 Nov 2010

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

20 Nov 2010

- added support semilogx, semilogy, loglog plots

22 Nov 2010

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

08 Dec 2010

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

11 Dec 2010

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

20 Dec 2010

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

05 Jun 2011

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

Contact us