File Exchange

image thumbnail

Controllable tight subplot

version (486 KB) by Nikolay S.
Subplot wrapper, allowing better figure utilization, by tighter axes.


Updated 31 Jul 2016

View Version History

View License

Allows fully utilizing the figure surface by defining margins, by wrapping the existing subplot function. Unfortunately the original subplots function spends lavishly the figure area , without allowing the user to control it in a convenient manner. In addition to subplot functionality (fully supported) the user can define margins, which result in larger axis presented in the figure. Default margins are 1% of figure area.
Thanks goes to a colleague- Adi N., who both urged me to write this function down, and assisted in writing it.
Hope you will find this one useful.
Some subplot functionality s now supported. However, I must mention this looks quiet buggy to me. Check this out:
subplot(1,2,1,'Position',[0.1,0.1,0.3,0.3]); % 'Position' is ignored!!!

Cite As

Nikolay S. (2021). Controllable tight subplot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (24)

Selma Kemmerer

Marshall Trout

Natalie Cole

Hugo Trentesaux

silvestro mp

Jeyson Ardila

Thank you very much, just what I was looking for

Jen-Hao Ou

Excellent function! Very useful when handing numerous figures.

Diego Vieira

Great syntax and it just works!

Shahar Grossbard

Afshin Aghayan


Space for labels should be somehow automatically included. You have to redefine margins for different size of the figure.

Uri Cohen

Nikolay S.

Hi Neil Tandon.
Thanks for the flattering grade. I am personally using Windows "Sniping Tool" to export relevant figure region or alternatively Matlab save figure to apply to multiple figures.

Neil Tandon

Works great for my needs. A good companion to this is the export_fig package, which makes it easy to crop out any whitespace on the figure edges. (I saw other comments about wanting to do this.)

Nikolay S.

Good morning Varoujan.
You're correct. I will add this change soon :).


A minor correction to the demo script - for it to work correctly, replace the line:



[currDir, ~, ~] = fileparts(which(mfilename));
dir_file = dir(fullfile(currDir,'*.jpg'));

Without this change, the demo script looks at the current working directory and NOT the directory where the demo_subplot_tight.m file is. With the change above, one gets the location of the .m file and uses it in the dir command.

Felipe G. Nievinski

I love that it can replace the built-in subplot with minor modification in the caller script. I wish it could handle margins in addition to gaps though.

Nikolay S.

Hello AMVR.
First of all, there is no need for repeating your message. It is perfectly clear when written once.

To the point- your script's issue can be solved by setting the figure size to the desired dimentions. Either repalce the figure command with : "figure('Position', [0,0,240,291*2]);",
or add :
"set(gcf, 'Position',[0,0,240,291*2])";
after your script.
I trust you will know where do the figure "position" parameters come from...
Regarding "subplot_tight" functionality as I see it. subplot_tight gola is to devide the figure to subplots spaced by margins specified by the user. Unfortunately, in case of image data (and some other cases) I have no way of knowing in advance what are the appropriate figure dimensions (as plotting commands follow subplot_tight). This will work differently with plot commands btw.

To see for yourself run the following command "subplot_tight(1,2,1, [0,0])" and verify that the axis "subplot_tight" generates have no (0) margins. The following "imshow" command in your script, actually places into the axis a plot with smaller dimensions, which results in margins you're so unhappy about.
So at this point setting appropriate figure size is under users responsibility. Actually it is not too hard to deal with, but unfortunately I found no way to do it inside subplot_tight scope.
Hope I've unswered your question.
Best regards.


Could you comment on why the script below using your code does not work ? MATLAB displays extra vertical space even though I asked for empty margins:

I = imread('pout.tif');
margins = [0 0];
subplot_tight(1,2,1, margins)
subplot_tight(1,2,2, margins)


Could you comment why the script below using your code does not work ? MATLAB displays extra vertical space even though I asked for empty margins:

I = imread('pout.tif');
margins = [0 0];
subplot_tight(1,2,1, margins)
subplot_tight(1,2,2, margins)

Alan B

Yes, that's right. This is how subplot.m works - it creates new axes on a figure, but it also simply sets existing axes as current, if the same mnp value is given. I'm not sure how subplot accomplishes this, but using tags is a quick solution that will work most of the time.

Nikolay S.

Hi Alan.
Thank you for your comment and rating. Could you explain in more detail the feature you want to be added? Assuming subplot is called with [m,n,p] do you want to take care of the case where subplot_tight is called with same []m,n,p] ? You want only the axis CData to be replaced, instead deleting current axis and creating a new one instead? Please elaborate.

Alan B

Useful, concise function. It would be more useful if calling repeatedly with the same parameters did not destroy the previously-created axes (this is how subplot behaves). I did this by tagging the subplot_tight axes, then checking if an axes with that tag already exists on the current figure.

Egon Geerardyn

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

Community Treasure Hunt

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

Start Hunting!