File Exchange

image thumbnail


version (57.8 KB) by Marc Jakobi
More reliable implementation of the option "expand axes to fill figure"


Updated 13 Nov 2019

From GitHub

View Version History

View license on GitHub

More reliable implementation of the option "expand axes to fill figure" in the Export Setup... settings.
Works with multiple subplots and usually does not distort objects such as colorbars. This function attempts to automatically remove white space in figures by expanding the axes objects to fill the figure. The available option in the "export setup" figure menu sometimes distorts the axes and often does not work at all if there is more than one axes or a colorbar. The common fix is to manually change the position of each axes, which can be a tedious process. This function attempts to automate the process while keeping the syntax as simple as possible. It automatically removes the white space of most figures with multiple subplots, superimpozed axes objects and colorbars without distorting the axes. The zip file contains the function and a script with examples of use.
Syntax: expandaxes(h)
expandaxes(h, fHor, fVer) - For manual adjustment of the distance between subplots
Input arguments:
- h: Figure handle
- fHor: Factor for the distance between subplots in horizontal direction (Default: 1)
- fVer: Factor for the distance between subplots in vertical direction (Default: 1)

a) General rule of thumb for the order of execution when calling expandaxes:
1) Set objects, FontSizes, etc.
2) Call expandaxes
3) Other manipulations of axes and colorbar positions
b) By setting h.SizeChangedFcn = 'expandaxes(gcf, fHor, fVer);'; this function can be called with exery resize of the figure h.

Cite As

Marc Jakobi (2021). expandaxes (, GitHub. Retrieved .

Comments and Ratings (12)

Marc Jakobi

Hi Elisabeth.
Could you please upload a script I can use to reproduce the issue?
I maintain the project on GitHub, so it would be best if you open an issue there:

Elisabeth Ötsch

Hey Marc! Thank you for this great Function. Is there a way how to expand axes of subplots until >sgtitle<? UNtil now it ignores those title and all axes expand until the top.

Marc Jakobi

Hi Jeon. Thanks for the feedback. I have just pushed a fix.

YoungBae Jeon

Thanks for the GREAT CODE! By the way, I found a small issue. I think the code doesn't work for single column or single row such as subplot(1,3,i) or subplot(3,1,i).

Marc Jakobi

Hi Daniel.
I have released an update with a fix :).
Thanks for letting me know!


Hi Mark. thanks for the code! It seems that the code does no work for subplot(n,m,i) with n different of m. For example it fails for:
>> for i=1:6 subplot(3,2,i); end; expandaxes(gcf)

Elliott Wise

Marc Jakobi

Hi Roland. Thanks for the hint! I have been having unexpected behaviour with colorbars for a while. Thanks to you I may finally be able to get the behaviour I want!


I found a bug in line 415. The parameter must be named "c" instead of "CP". Nevertheless, works great. Thanks for the contribution!



Tjarko Tjaden

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

Inspired by: export_fig

Community Treasure Hunt

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

Start Hunting!