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

Axes LooseInset property

Posted By Yair Altman On March 24, 2010 | __21 Comments__

Last week, I wrote an article about the hidden/undocumented **LineSmoothing** plot property ^{[3]}. This week, I want to introduce another useful hidden/undocumented property – the plot axes’ **LooseInset** property. This follows on the wake of an email I received from a reader about this property, which had some new information for me (thanks Ben!).

Apparently, **LooseInset**, which is automatically set to a factory value of [0.13, 0.11, 0.095, 0.075], is used by Matlab axes to reserve a small empty margin around the axes, presumably to enable space for tick marks. These empty margins can be very annoying at times, especially when we have directly control on the axes contents.

figure; t=0:0.01:7; plot(t,2*sin(t));

If you set **Position** to [0 0 1 1], the labels are cut-off; if you set **Position** to something like [0.05 0.05 0.9 0.9], you can get the labels to show up, but if you now resize the image the labels may be cut off… Similarly, setting **TightInset** also does not work.

Theoretically, the solution should be to set **OuterPosition** to [0 0 1 1]. This is supposed to make the axes (including labels) take up the entire figure. However, it usually over-estimates the required margins, causing wasted space. Using **OuterPosition** also causes unexpected behaviors with sub-plots ^{[4]}.

Solution: simply set **LooseInset** to [0 0 0 0]:

set(gca, 'LooseInset', [0,0,0,0]);

To modify all future axes in the same way (i.e., have an empty **LooseInset**):

set(0,'DefaultAxesLooseInset',[0,0,0,0])

Clearing the **LooseInset** margins has a drawback: if the axes is zoomed or modified in such a way that the labels change, then the active axes plot region needs to shrink accordingly. For example:

When determining the size of the axes, it seems that Matlab takes into account larger of the documented ^{[5]} **TightInset** and the undocumented **LooseInset**. So, perhaps a better generic solution would be the one suggested ^{[6]} by another blog reader:

set(gca,'LooseInset',get(gca,'TightInset'))

Note that the **LooseInset** property was first reported on CSSM ^{[7]} back in 2007 (also here ^{[8]}). The **LooseInset** property has remained hidden and undocumented to this day (Matlab 7.10, R2010a), although it has even featured in an official MathWorks Technical Solution to a reported problem about unexpected axes sizes ^{[4]} last year.

p.s. – another undocumented property of Matlab axes, **ContentsVisible**, was described by Matt Whittaker in a comment ^{[9]} on my original article that introduced undocumented properties.

Categories: Handle graphics, Hidden property, Low risk of breaking in future versions

21 Comments (Open | Close)

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

URL to article: **https://undocumentedmatlab.com/blog/axes-looseinset-property**

URLs in this post:

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

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

[3] **LineSmoothing** plot property: **https://undocumentedmatlab.com/blog/plot-linesmoothing-property/**

[4] unexpected behaviors with sub-plots: **http://www.mathworks.com/support/solutions/en/data/1-1ZOG12/**

[5] documented: **http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axes_props.html#TightInset**

[6] suggested: **http://dopplershifted.blogspot.com/2008/07/programmatically-saving-matlab-figures.html#c3830299630471935212**

[7] reported on CSSM: **https://www.mathworks.com/matlabcentral/newsreader/view_thread/156326#394470**

[8] here: **https://www.mathworks.com/matlabcentral/newsreader/view_thread/140047#352929**

[9] comment: **https://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/comment-page-1/#comment-677**

[10] Plot LimInclude properties : **https://undocumentedmatlab.com/blog/plot-liminclude-properties**

[11] Panel-level uicontrols : **https://undocumentedmatlab.com/blog/panel-level-uicontrols**

[12] Matlab’s HG2 mechanism : **https://undocumentedmatlab.com/blog/matlab-hg2**

[13] Tab panels – uitab and relatives : **https://undocumentedmatlab.com/blog/tab-panels-uitab-and-relatives**

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

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

[16] : **http://undocumentedmatlab.com/blog/figure-toolbar-components#comment-13890**

[17] : **http://undocumentedmatlab.com/blog/axes-looseinset-property**

Click here to print.

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

21 Comments To "Axes LooseInset property"

#1 CommentByRalfOn March 31, 2010 @ 12:47 amNice to see that there is a simple solution for this problem and I do not have to write a Figure Resize Callback function for this purpose. However, in the case of “axis equal” the described procedure does not work since the figure position has to be set to produce a cropped window. Does anybody knows whether there is a simple method to compute cropped figure dimensions for equal axis with labels?

#2 CommentByOliverOn February 21, 2014 @ 3:18 am@Ralf – I would need this too.

#3 CommentByMaurizioOn April 1, 2010 @ 3:49 amIs there any similar defalut property like ‘DefaultAxesLooseInset’ for subplots?

#4 CommentByYair AltmanOn April 1, 2010 @ 4:06 am@Maurizio – there is only one

DefaultAxesLooseInsetproperty in the root (0) handle – there is no distinction between main axes and sub-plots, since sub-plots are simply a set of axes that are distributed (laid-out) nicely within the figure. So, setting theDefaultAxesLooseInsetproperty affects all the subplot axes.#5 CommentByVladiOn July 24, 2012 @ 2:49 pmThis statement:

set(0,’DefaultAxesLooseInset’,[0,0,0,0])

.. seems don’t work (R2012a)

#6 CommentByVladiOn July 24, 2012 @ 3:34 pmrecall on my previous post:

.. seems does work, but only at main axes level..doesn’t affect subplot behavior.., so gray wasted spaces between figure’s subplots remain as default..

#7 CommentByYair AltmanOn July 29, 2012 @ 2:28 am@Vladi – the

function overrides the defaultsubplotLooseInsetvalue and sets its own ([.13,.11,.095,.047] in normalized units). After the subplots are created you can override theirLooseInsetandOuterPositionproperty values to get the desired effect.#8 CommentBysundarOn August 21, 2012 @ 4:51 amThis (set(gca,’LooseInset’,get(gca,’TightInset’))) works well for me in case of plots generated using the plot command. But it does not work for me for figures generated using imshow command.

Any suggestions?

Thanks

#9 CommentByYair AltmanOn August 21, 2012 @ 4:59 am@Sundar – it does work for

, but it is possible that you see no difference and the reason is thatimshowpreserves the image aspect ratio and other such factors that affect image display in the axes. If you’ll doimshowI think you will see this more clearly. Also, resize the figure window to see the figure axes change its size.set(gca,’Visible’,’on’,’Box’,’on’)#10 CommentByDimsOn June 9, 2013 @ 3:23 pmBoth LooseInset and Visible didn’t work for me.

The following code works samely with and without any sets.

#11 CommentByYair AltmanOn June 9, 2013 @ 3:38 pm@Dims –

LooseInsetdoesn’t work on subplots, only regular axes; try settingVisibleafterimshow()#12 CommentByChrisOn January 21, 2014 @ 6:51 amI’ve noticed that

sometimes seems to work best after a short delay following resizing and positioning a subplot. If it doesn’t seem to make much difference when first applied, I use

I’m guessing this ensures the plot has been rendered on-screen before the command is applied.

#13 CommentByYair AltmanOn January 21, 2014 @ 6:52 am@Chris – I assume that

would be more appropriate thandrawnowin this casepause#14 CommentByRichard HodgesOn June 5, 2014 @ 11:02 amIf an imagesc in a figure has a colorbar, setting LooseInset to [0 0 0 0] causes the colorbar to be outside the figure, hence not visible. LooseInset can be set to some other value that keeps the colorbar visible, for example [0 0 .1 .01], but if the figure is resized the value of the 3rd element of LooseInset needs to be different. This could possibly be addressed with a resize callback function but I have not been able to figure out how to automatically determine a good value for LooseInset. Anybody have a solution?

Thanks,

Richard

#15 CommentByGeraldOn July 9, 2014 @ 8:30 amI’ve noticed that if I use

`set(gca,'LooseInset',get(gca,'TightInset'))`

to remove the white space, after I save the figure and reopen the figure the xlabel is cut-off for larger sized figures (1206 x 785 pixels) because MATLAB changes theActivePositionPropertyfrom`'outerposition'`

to`'position'`

, and MATLAB also reduces the figure vertical size by 6 pixels. Anybody else run into this?#16 CommentByYair AltmanOn July 9, 2014 @ 8:41 am@Gerald – odd. A possible workaround is to set the figure’s

CreateFcncallback property to a function that fixes this. Whenever the figure will then be opened, this callback will execute, fixing this (and any other) oddity. I^{[16]}to recreate toolbar customizations that would otherwise get lost across figure save/load.#17 CommentByFasilOn December 31, 2015 @ 5:07 amThanks a lot.

#18 CommentByNateOn May 25, 2016 @ 8:16 pmLike others, I’m struggling to use this to get rid of white space in subplots. Perhaps an example would help? And is it best to resize the figure before apply this commands?

Many thanks,

#19 PingbackByMake MATLAB Plots fill the whole window – zachlge.orgOn November 14, 2016 @ 10:51 am[…] And voila, your plot should fill the whole window without creating much whitespace, which is perfectly fine for exporting plots for documentations. You can also check out some more propertys at

^{[17]}[…]#20 CommentByJohnnyOn March 9, 2017 @ 12:28 pmThanks a million, super useful

#21 CommentByssOn June 5, 2017 @ 10:29 amIt doesn’t work when axes in uipanel?

Why?