Code covered by the BSD License  

Highlights from
On-figure magnifier

4.86667

4.9 | 30 ratings Rate this file 149 Downloads (last 30 days) File Size: 15.9 KB File ID: #26007
image thumbnail

On-figure magnifier

by

 

02 Dec 2009 (Updated )

Powerful on-figure magnifier, suitable for the publication of compact graphical results

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

This utility provides a powerful zooming tool specially designed for the documentation of original and zoomed 2D graphics of images on the same plot.

It is composed of two blocks (the secondary axes and the magnifier). The secondary axes, which can be arbitrarily allocated within the limits of the figure, displays the area marked by the magnifier.

The highly versatile interface of the 'on-figure magnifier' offers a world of possibilities in a broad range of applications and fields. The Keyboard/mouse or programmatic configuration of this tool allows the user to set her or his own graphic style, to suit the requirements of any publication's editor. More details on interfacing and properties can be found in the .m file.

EXAMPLE: (for usage examples, run 'magnifyOnFigure_examples.m')
         
PROPERTIES:
       'secondaryAxesFaceColor': ColorSpec
       'edgeWidth' Color of the box surrounding the secondary
                    axes, magnifier and link. Default 1
       'edgeColor': Color of the box surrounding the secondary
                    axes, magnifier and link. Default 'black'
       'displayLinkStyle': Style of the link. 'none', 'straight' or
                    'edges', with 'straight' as default.
       'mode': 'manual' or 'interactive' (allowing
                    adjustments through mouse/keyboard). Default
                    'interactive'.
       'units' Units in which the position vectors are
                    given. Only 'pixels' currently supported
       'initialPositionSecondaryAxes': Initial position vector ([left bottom width height])
                    of secondary axes, in pixels
       'initialPositionMagnifier': Initial position vector ([left bottom width height])
                    of magnifier, in pixels
       'secondaryAxesXLim': Initial XLim value of the secondary axes
       'secondaryAxesYLim': Initial YLim value of the secondary axes

HOT KEYS (active if 'mode' set to 'interactive')

 -In a figure with multiple magnigier instances
       'Tab': Switch the focus from one magnifier instance
                    to the next one on the current figure.
       'Mouse pointer on secondary axes or magnifier of a tool+double left click'
                    Regain focus

 -On the focused magnifier instance
       'up arrow': Moves magnifier 1 pixel upwards
       'down arrow': Moves magnifier 1 pixel downwards
       'left arrow': Moves magnifier 1 pixel to the left
       'right arrow': Moves magnifier 1 pixel to the right
       'Shift+up arrow': Expands magnifier 10% on the Y-axis
       'Shift+down arrow': Compress magnifier 10% on the -axis
       'Shift+left arrow': Compress magnifier 10% on the X-axis
       'Shift+right arrow': Expands magnifier 10% on the X-axis
       'Control+up arrow': Moves secondary axes 1 pixel upwards
       'Control+down arrow': Moves secondary axes 1 pixel downwards
       'Control+left arrow': Moves secondary axes 1 pixel to the left
       'Control+right arrow': Moves secondary axes 1 pixel to the right
       'Alt+up arrow': Expands secondary axes 10% on the Y-axis
       'Alt+down arrow': Compress secondary axes 10% on the Y-axis
       'Alt+left arrow': Compress secondary axes 10% on the X-axis
       'Alt+right arrow': Expands secondary axes 10% on the X-axis
       'PageUp': Increase additional zooming factor on X-axis
       'PageDown': Decrease additional zooming factor on X-axis
       'Shift+PageUp': Increase additional zooming factor on Y-axis
       'Shift+PageDown': Decrease additional zooming factor on Y-axis
       'Control+Q': Resets the additional zooming factors to 0
       'Control+A': Displays position of secondary axes and
                    magnifier in the command window
       'Control+D': Deletes the focused tool
       'Mouse pointer on magnifier+left click' Drag magnifier to any
                    direction
       'Mouse pointer on secondary axes+left click' Drag secondary axes in any
                    direction

Acknowledgements

Zoom Box, Magnify, Magnifyrecttofig, and Jeweler's Loupe inspired this file.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (43)
23 Jan 2014 Sathish Kumar

Very good work.Genius of you!

06 Jan 2014 Edward Byers

This is pretty genius - thanks

09 Dec 2013 Hayden

I made an account to rate this great tool, works like a charm!

03 Dec 2013 rebaz Ahmed

thanks a lot sir, you really helped me and i appreciate it very much. wish you the best.

29 Nov 2013 Leo

Thank you very much! It is very useful!

19 Nov 2013 maryam  
04 Nov 2013 Isaac Wang

Brilliant job! Thanks a lot! I can create my figure easily by using it.
But there are some bugs in it. When I saved the figure as a .eps file, it accidentally created something like a black rectangle on the south-west corner. Hope anyone can fix that.
Thanks in advance!

06 Oct 2013 Mingyang Li

Fantastic work, very useful

04 Oct 2013 Eneko  
12 Sep 2013 Ali Hummos

Great tool!! I join my voice in the issue of the magnifier window disappearing on export though. A solution would be amazing!

04 Sep 2013 Nickolas

This is a really nice tool, thanks! I would like to echo Daly's comment about the known bug regarding the position of the magnifier window after printing the figure. This function is perfect for what I am doing, but I need to be able to export the figure and it seems impossible without a fix for the magnifier window position problem. Do you have any suggestions for work-arounds? Thanks again.

06 Aug 2013 Dicle Hasdemir

Thanks for the tool! I have a small question. I have 5 subplots in one figure window. When I use the code either in manual or interactive mode, the zoom window gets stuck in the first subplot. Even when I give the coordinates for the zoom window inside the second subplot, it is located near the border of the first subplot. I am using Matlab R2012b on Ubuntu 12.04. Do you have any idea on this issue?
Thanks in advance!

02 Aug 2013 Leticia Zamora

Amazing tool. Congratulations!!

15 Jul 2013 Jonathan Lister

Good tool. Could you add a keypress action to disable the tool i.e. press 'Esc'? Also I notice that when I zoom the magnify plot the box outine on the original axes doesn't change. This causes a disconnect between the inset and the box outline. The tool would be nearly perfect if those two issue were addressed. As it stands right now there doesn't appear to be a good way to exit the tool. Maybe I've missed something?

03 Jun 2013 Sérgio

Amazing! Congratulations!

19 May 2013 Daly

Very attractive! Just a problem that i I have encountered when i try to export the figure in eps format with print function. The magnifier shrink and move out the axes to the right corner of the generates image. The link between the magnifier and the secondary axes is not affected and it kept its position. The only problem is with the magnifier. Can anyone helps me please.
Thanks for Mr David and all they contribute here.

12 May 2013 Qiao Liyong

Thank you for sharing your code.
I hade used this code.
'initialPositionSecondaryAxes':
Initial position vector ([left bottom width height]),the unit is 'pixels' ,the width and height can be right,but left and bottom are wrong. the same problem is with 'initialPositionMagnifier'.
For exemple:
[Tempfirst,rect] = imcrop(Picfirst);
figure(2);imshow(Picfirst);title('Picfirst and Tempfirst');
magnifyOnFigure(figure(2),'magnifierShape','rectangle',...
'initialPositionMagnifier',[rect(1),ysize-rect(2)-rect(4),rect(3),rect(4)],...
'initialPositionSecondaryAxes',[xsize-rect(3),ysize-rect(4),rect(3),rect(4)],...
'mode','interactive','edgeWidth', 3,'edgeColor', 'red');

10 Jan 2013 Shuster

this tool is exactly what I had been looking for. However, I found issues with magnifying 3D plots. Can you please check and update it for 3D plots with an example?

thanks

07 Dec 2012 Pancho

Many, many Thanks!!.... I have never seen better script!... I love it!

21 Nov 2012 Alexander

Very nice and helpful

08 Nov 2012 Karl

Hi David,

and thank a lot for this function! It works really well. Only one thing - and I'm not sure whether I'm doing something wrong or if it's actually a small bug in your code: When I select the 'DisplayLinkStyle' to be 'edges' I get an error in line 1918 (version 1.17 on MATLAB 2009b). I made a little change to the code to fix this problem (at least for me):

Line 1918:
----------
change

linkHandle = appDataStruct.linkHandle;

to

linkhandle = appDataStruct.linkHandle;

That was a typo, I guess! And

Line 1908:
----------
change

if isempty( appDataStruct.linkHandle )

to

if isempty( appDataStruct.linkHandle ) || length(appDataStruct.linkHandle<4)

Maybe this is of use for anyone... :)

Cheers from Germany and regards,

Karl

21 Aug 2012 Caped Crusader  
07 Jul 2012 James

Thanks for sharing this nice code.
A bug: when the figure is saved as an .eps the rectangle framing the section of curve to be enlarged disappears in the saved version.

12 Apr 2012 Amin

how I can enlarge and display in a window Figure

09 Feb 2012 Merc Shahraki

Thanks alot
its very useful

10 Nov 2011 Tian Pan

good

13 Sep 2011 Pieter van den Berg  
05 May 2011 Jose Ercolino

Very nice work.

04 May 2011 Xuan  
16 Apr 2011 mythili

hi..great work.....
its very useful..
thank u...

22 Mar 2011 ben harper

hello,
i'm sorry if this feature is already added but i couldn't see.

how can i magnify on subplots?
i added between the subplots but it couldn't work.
thank you.

figure
subplot(2,1,1)
...
subplot(2,1,2)
...
magnifyonfigure
...
subplot(2,1,3)
...

23 Feb 2011 Royi Avital

This is Amazing!

I wish you added support for more units (The axis units would be great).

22 Sep 2010 suraj

Cheers to the great work. I have a couple of questions to ask. i) How do I set the Y limits of the magnifier to match the plot area height? ii) Can I use the magnifier as a push button callback in a GUI? Thanks in advance.
Suraj

14 Jul 2010 David Fernandez-Prim

Hi Corey,

1. You can easily change the color of the border of the magnifier and secondary axis through the property 'edgeColor', which by default is set to black.

2. At this moment, the tool does not provide a handle at calling time. However, there are several supported Hot Key combinations that might be useful for your purpose. 'Control+D', for instance, can be used to delete the tool from the current figure.

I hope that helps!

Regards

13 Jul 2010 Corey Kelly

This is great! Two simple questions:
1. If I wanted to change the color of the borders of the magnifier and the secondary axis, where in the code would I make the change? As is, they're showing as black, and my images are predominantly black, making them difficult to see.
2. I'm integrating the magnifier as part of a GUI, and would like to be able to toggle it on and off. It's not clear to me how to remove the magnifier once it has been initialized. Is it possible to create a handle when the magnifier starts, which can be used as an argument to a delete() call?

04 Jun 2010 theoxarhs alex

Everything is great, except for the fact than when semilogy is used instead of plot, the zoom box is not placed correctly on the graph. Is is misplaced a little bit lower than it should be.

Any changes to overcome this problem?
Please feel free to mail me at theoxarhs at google.com if a simple change could resolve this bug(?).
Thanks in advance.

01 Jun 2010 jvc

While this looks like a nice tool, I experienced problems with it due to its method of use of the figure's userdata field. It appears that the tool ignores the possibility that another application might already be using the userdata and hence when it encounters unexpected data in the field it crashes. I've always tried to treat userdata as shared space - when I read it I assume that there might be data there that I did not put there and I alway try to leave existing data unchanged when I resave to userdata. This would be a useful upgrade for this script.

04 Apr 2010 David Fernandez-Prim

Dear Ben,

The functionality you request is already available in the tool.

In interactive mode, if you hit 'control + A', you will get information about the current position of secondary axes and magnifier on the command window of MATLAB.

After that, you only need to set the properties 'PositionSecondaryAxes' and 'PositionMagnifier' to the given values on the next call to this function.

I hope this answers your request :)

04 Apr 2010 ben harper

First of all, Thank you for your great work :)

After plotting the magnifier, if we are in interactive mode, we can change the size of magnifier and secondary axes.

Is it possible to add a key to script, when we press it it would show "final PositionSecondaryAxes" and "final PositionMagnifier"?

09 Feb 2010 Kris Gryte

David:

Thank you for the response. Regarding switching from one inset to another, indeed, 'tab' does activate the next inset, regardless of the axes; however, the situation I envision is having say, three insets on one subplot, two insets on another subplot, three insets on yet a third subplot--for whatever reason and despite aesthetics. Maybe now the problem becomes evident. If, say, I am on the third subplot and the 8th inset, in order to access the 7th inset, I have either to (i) cycle through the other 7 insets, or (ii) use my mouse to double-click. I, personally, like shortcuts and avoid using my mouse (habit), thus, I find it more convenient to cluster my insets by axis and, for the active axes, I cycle between my 'local' insets. Further, depending on the order of placing insets on whatever axes, two insets on an axes may not be next to each other in the 'tab' order. Third, a shortcut for activating axes allows for placing insets, not necessarily even activating insets, on different axes, without mouse-clicks. And finally, I usually do most things GUI-based, in which I provide tags for each axes/inset and then provide dialogs (e.g., listboxes, etc) to allow the user to select specific axes and/or insets for manipulation. But, of course, this is my preference.

As for other notes:

(i) I like shortcuts, but, for other users, they love their mouse. Hence, an easy extension is simply to provide a 'uicontextmenu' for each created axes, in which the menu, say, for the 'magnifier' provides the various shortcuts/commands particular to that axes (resize, magnification, etc), and similarly for the inset. A user may then right-click an inset and simply select to resize or expand or whatever. And further, by listing 'Increase X-Mag (alt++)', or whatever the shortcut is, the user has a reference to remind his/herself of the various shortcuts.

(ii) Another extension may be the ability to synchronize two insets. For example, suppose I have two subplots and one inset per subplot. Further, the two subplots contain related information, and I would like to magnify a single region in both subplots. In this implementation, I have to individually drag each magnifier to the desired region separately, and then manually attempt to align the two axes. This may, or may not, be easy depending on the length of the displayed data and the resolution of the axes. A nice extension would be (yet another shortcut!) to link two insets together, such that they move in step. A similar concept has been implemented by 'Dual Cursors', which may be found on the Central File Exchange. Or, another possibility is two insets on the same axes, but separated by a given spacer. If I link them, then when I move one axis to the left by 10 units, the second axis also moves to the left exactly 10 units. Similarly for magnification, resizing, et cetera. Hence, having the ability to apply shortcuts/operations to more than one inset at a time, accomplished either using 'loops' or the 'set' function. Of course, if performance was already an issue, this suggestion would only compound matters.

Thanks again for the fixes and comments. Great work.

-Kris

11 Jan 2010 David Fernandez-Prim

Kris, thanks a lot to you too for your comments. In version 1.16, you could already switch from one inset to the next one in a figure( by pressing 'tab' of doble-clicking the inset to focus), regardless they are on the same axes or not.
Maybe this feature could already suit your needs.

I'm currently working on the performance issue, which is not an easy one. I'll let you know the status. Please don't hesitate to report any other issue/bug you might find.

- David

10 Jan 2010 Kris Gryte

Thank you for addressing the issues reported earlier. The various bug fixes, additional support, and increased functionality are greatly appreciated.

Perhaps a further generality might be the following:

- Multiple insets across multiple subplots within the same figure. I have addressed this by including another shortcut which activates the next (or previous) axes.

Thanks again.

-Kris

18 Dec 2009 Kris Gryte

Very nice work. A few things:

(i) Hack: in order to delete the magnifier, secondary axes, and link, I simply added a new keyboard shortcut (alt+C), and then directed to a sub-function which deleted the desired axes handles and restored the figure and main axes default settings.

(ii) Problem: I have noticed the following correlation. When the figure window is small, say, 10 in x 8 in, the magnification, motion, and shortcuts work wonderfully. This changes, however, when I expand the figure window so it is maximized (i.e., roughly, 24 in by 11 in). Each time I attempt to used the magnification function on the expanded figure window, my Matlab session crashes, and if this system is able to remain responsive, I receive repetitive warnings that I have exceeded by 'listener recursion limit'. This issue is not alleviated when I reset the recursion limit to something extreme (e.g., from 500 to 10000). Hence, something within the configuration, perhaps the 'buttonmotionfcn' or some other callback which continuously updates/has eventdata, seems to find the increased window size rather disagreeable.

This stated, I was curious if you have a possible workaround for issue (ii).

(iii) Generalization: a nice, albeit more complicated, addition would be the ability to incorporate more than one plot inset for a given axes and/or figure. Possibly, by including another shortcut command, one could cycle between making a given secondary axes/magnifier active and inactive. For example, store all axes within a figure, and then upon the shortcut command, provide a 'listdlg' to allow the user to choose which axes to make active (which may or may not be a magnifier). If a magnifier axes is selected, then make the associated handles active, et cetera.

The function has outstanding functionality and use, and I am eager to have it fully and smoothly incorporated in my GUI. Thank you for this submission.

-Kris

Updates
03 Dec 2009

Solved bug when 'displayLinkStyle' is set to 'edges'

04 Dec 2009

Added possibility to select target axes in a multiaxes figure

06 Dec 2009

Solved bug appearing when any of the axes are reversed. It also works on images now.

07 Dec 2009

Cosmetic changes

08 Dec 2009

Solved bug when axis mode is set to other than 'normal'.
Besides, added 'frozenZoomAspectRatio' zoom mode, useful when working with images.

08 Dec 2009

Solved bug when the contents of the target axes are other than 'line' or 'image'

07 Jan 2010

Added 'delete' functionality.
Added support to multiple instances of the tool on the same axes.
Solved several minor bugs

07 Jan 2010

Solved bug when resizing the host figure

07 Jan 2010

Improved help documentation

28 Mar 2010

Tool identification feature added. Now, 'Control+I' shows/hides the tool identifier (red background color when the tool has the focus, black otherwise)

Contact us