- Undocumented Matlab - https://undocumentedmatlab.com -

Determining axes zoom state

Posted By Yair Altman On November 10, 2011 | 11 Comments

A couple of days ago, a reader of Matlab’s official Desktop blog asked [3] whether it is possible to determine if an axes has been zoomed or not.

I have encountered this question myself some time ago, when I tried to customize a radar plot: The grid in radar plots does not automatically re-draw when the plot is zoomed, as in regular rectangular plots. Instead, the radial (angle) and circular (range) grid lines are statically painted when the plot is created, and remain fixed when the plot is zoomed or panned. Therefore, if we zoom in on a small-enough plot segment, we will not see any grid lines at all. It would be beneficial to repaint the grid-lines upon every zoom and pan event. I will not dive into the details today, but the important thing for today’s article is that the algorithm needed to know whether or not the axes is currently zoomed or not.

The official response is that this information is not readily available. We could of course store the axes limits somewhere and then compare them to the current limits in run-time. This will cause complications if the plotted data (and thereby the axes limits) change automatically during program use, even without any zooming/panning. It is also problematic if the user resets the zoom limits (as Jiro has correctly pointed out [4]).

Today I’ll highlight another possible solution, that perhaps not many Matlab users are familiar with: Apparently, whenever zooming or panning occur, a hidden ApplicationData object is automatically added to the affected axes, with information about the original axes meta-data: limits, aspect ratio, camera info and view angles. This object is also automatically updated whenever we use zoom reset to reset the zoom limits.

So basically, all we have to do in run-time is to compare our current access limits with the stored info: if they are the same (or if the app-data object is missing) then the plot is unzoomed and unpanned; otherwise it is. So simple.

origInfo = getappdata(gca, 'matlab_graphics_resetplotview');
if isempty(origInfo)
   isZoomed = false;
elseif isequal(get(gca,'XLim'), origInfo.XLim) && ...
       isequal(get(gca,'YLim'), origInfo.YLim) && ...
       isequal(get(gca,'ZLim'), origInfo.ZLim)
   isZoomed = false;
   isZoomed = true;

This still does not solve the problem of limit changes when the plot data is changed, but it may perhaps be a bit easier to use than manually storing the limits before plotting. (And yes, of course the if-else statement above could be made a one-liner logical construct, but I think this way is more readable).

Zooming and panning add some additional data to ApplicationData, and also use/modify some other hidden properties of the axes handle (use the getundoc function [5] to see them). If you use one of them for any useful purpose, please report your usage in a comment below [6].

Categories: Handle graphics, Hidden property, Medium risk of breaking in future versions, Stock Matlab function, Undocumented feature

11 Comments (Open | Close)

11 Comments To "Determining axes zoom state"

#1 Comment By Richard On November 12, 2011 @ 4:03 am

Hi Yair, I am the original asker on the blog, I thought I’d repeat my comment here. The main reason I want to know this is because I’m developing a code to automatically produce ‘tight’ figures without excess space around them for publication. I also want this code to work for subplots by getting the minimum required bounding box (based on the ‘TightInset’ and ‘Position’ properties) of every axes in the figure. My problem is, if a 3D plot has been zoomed, these properties are no longer useful, and the figure should be ignored, or left as it is. 2D plots that have been zoomed are fine, as the axes stay where they are in the figure, but the whole view of the figure changes if any 3D axes have been zoomed. I have found a way to determine if an axes is 3D, using the view function. Perhaps using your tips I can get the last part.

P.S. I’ve read quite a few of your blog articles over the last year or so, it’s been very useful, thanks!

#2 Comment By Yair Altman On November 12, 2011 @ 8:34 am

@Richard – take a look at the undocumented hidden axes property LooseInset: [13]

#3 Comment By Aleksander Hammernes On May 9, 2014 @ 4:46 am

Would it be possible to get the zoom area ?
let say you activate the zoom function in a axes, normally this off course works fine, you can click and drag your mouse inside
the axes, and it zoom`s the axes to your wishes. how ever not in my gui…… in need to replot to make it work for me, so is there a
way to “capture” the x and y limits from the drag dragable zoom ?

#4 Comment By Yair Altman On May 9, 2014 @ 6:47 am

xlimits = get(hAxes,'xlim');
ylimits = get(hAxes,'ylim');

#5 Comment By Adam On October 10, 2014 @ 3:14 am

Hi Yair. Do you know if something has changed in R2014b that would cause this to no longer work when the figure containing the axes is not yet visible when zoom reset is called? It is one of a number of related things in my code that has broken from R2014a all seemingly related to whether or not a figure is actually visible yet or whether it is the figure in focus. I fixed this case by just checking if the zoominfo object is empty (it is now in R2014b, it wasn’t in R2014a when triggered from the OpeningFcn) and another case where creating a zoom object without an explicit figure used to match to the correct figure by default in R2013a, but picked up the figure that spawned it instead in R2014b was easy to fix.

I couldn’t see anything in the described graphics changes though that would explain this behaviour. My troubles with findjobj also seem to relate to this type of issue, seemingly EDT effects that were not present in R2014a, but now break the code in R2014b. In that case I just had to stop using the functionality, at least until I have more time to investigate. The axis zoom state information in this blog has been very useful to me though so thank you for sharing.

#6 Comment By Yair Altman On October 10, 2014 @ 3:16 am

@Adam – there are numerous small changes in 14b that are not officially documented. I guess this is yet another example.

#7 Comment By Eduard Braun On September 12, 2015 @ 3:30 pm

This is a very interesting way to determine if the axes are zoomed.

However I’m wondering if it is possible to do this the other way round: Start with unzoomed axes and programmatically bring them into a state as if they were zoomed in by the user?

For clarification: If I simply set XLim and/or YLim on the axes I can zoom in a portion of the graph but the user can not zoom out anymore… Therefore it’s not the same as if the user zoomed in (since then he obviously can zoom out again).

#8 Comment By Yair Altman On September 13, 2015 @ 12:17 am

@Eduard – you can use the zoom function with a certain factor, for example, zoom(2.5)

#9 Comment By Eduard Braun On September 13, 2015 @ 7:03 am

Thanks for feedback!

I had looked at zoom before but sadly it doesn’t allow set specific numeric limits on the axes which makes it useless for many cases.

However it turned out the actual solution is pretty simple:
1. Set the limits of the axes to the desired “zoomed out” state by setting appropriate limits.
2. Use zoom(figure,'reset') – this is were the “magic” happens and the current state of the figure is internally saved as the completely zoomed out state.
3. Achieve the desired zooming by simply setting appropriate limits on the axes.

Steps 1 and 3 look redundant (and one would actually end up with the same visible content when just executing step 3.), however if one omits steps 1. and 2. it would not be possible to manually zoom out of the figure again!

#10 Comment By Adee Ran On May 29, 2016 @ 6:32 pm

Thanks for exposing this (yet another) undocumented feature!

I used a variant to set the “original zoom” of only the horizontal axis – which “zoom reset” does not seem to support:

rpv = getappdata(gca, 'matlab_graphics_resetplotview');
rpv.XLim = [tmin tmax]; % Use values calculated elsewhere
setappdata(gca, 'matlab_graphics_resetplotview', rpv);

Works like a charm.

#11 Comment By tim On July 4, 2016 @ 7:10 am

The suggested solution seems to fail when zoom is constrained to only one axis and/or zoom mode is changed. Not all lim fields exist in ‘matlab_graphics_resetplotview’ for constrained axis zoom (so testing the others will throw an error). It also seems that fields are not updated when zoom mode is changed (eg from horizontal to unconstrained). A change in mode can however be detected by comparing gca’s limmode for each axis to those in matlab_graphics_resetplotview. But I don’t think it is then possible to tell if the current zoom value (ie lim) is different to its original setting if that axis did not have its zoom mode on originally.
A minimalist solution would be to store the 3 axes’ limits on creation.

NB r2016a

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/blog/determining-axes-zoom-state

URLs in this post:

[1] Image: http://undocumentedmatlab.com/feed/

[2] email feed: http://undocumentedmatlab.com/subscribe_email.html

[3] asked: http://blogs.mathworks.com/desktop/2007/12/10/focused-on-zooming/#comment-8204

[4] pointed out: http://blogs.mathworks.com/desktop/2007/12/10/focused-on-zooming/#comment-8206

[5] getundoc function: https://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties/

[6] comment below: https://undocumentedmatlab.com/blog/determining-axes-zoom-state/#Respond

[7] Multi-column (grid) legend : https://undocumentedmatlab.com/blog/multi-column-grid-legend

[8] Controlling plot data-tips : https://undocumentedmatlab.com/blog/controlling-plot-data-tips

[9] getundoc – get undocumented object properties : https://undocumentedmatlab.com/blog/getundoc-get-undocumented-object-properties

[10] Getting default HG property values : https://undocumentedmatlab.com/blog/getting-default-hg-property-values

[11] Undocumented view transformation matrix : https://undocumentedmatlab.com/blog/undocumented-view-transformation-matrix

[12] Using linkaxes vs. linkprop : https://undocumentedmatlab.com/blog/using-linkaxes-vs-linkprop

[13] : http://UndocumentedMatlab.com/blog/axes-looseinset-property/

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.