Allows to control the distance between subplots, add labels on rows/columns only and more.

% simple example:

subplot1(2,3);

subplot1(1); plot(rand(10,1));

subplot1(2); plot(rand(10,1));

...

Eran Ofek (2021). An improved subplot function (https://www.mathworks.com/matlabcentral/fileexchange/9694-an-improved-subplot-function), MATLAB Central File Exchange. Retrieved .

Created with
R14SP3

Compatible with any release

**Inspired:**
Spectral acceleration, velocity and displacement spectra, subtightplot

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Manuel EspinosaGreat improvement!

Shashwat TripathiI can get the Gap to change between the plots and how do I change the y limit

Naveen PathakFinally work ... need not to repeat the subplot1(2,3) like the matlab built in .. thanks for the excellent work !

Dr. H. OdensackHow to use use legend with 'subplot1' with matlab >2014

% look for

%--- move focus to subplot # ---

H = get(gcf,'Children');

% Add following lines

del = []; gg = 1;

for kk = 1:length(H)

if ~isempty(findobj(H(kk),'Type','Legend'))

del(gg) = kk;

gg = gg + 1;

end

end

H(del) = [];

Jihoon LeeIt's great. But there is a single problem about legend. When I run the code (in R2015b) like a one below

-------------------

for m = 1:n

subplot1(m)

plot(x,y)

legend('a','b','c')

end

-------------------

I always get the following error message.

-------------------------

Error using matlab.ui.Figure/set

Handles of type Legend cannot be made the current Axes.

Error in subplot1 (line 138)

set(gcf,'CurrentAxes',H(M));

Error in FigureMaster (line 23)

subplot1(k)

-------------------------

Please let me know how can i use legend with 'subplot1'

Andrey Ivanovandrew jorosGreat function! Thanks!

QUESTION: Is there a way to force the size of the subplot box to be perfectly square rather than a rectangle? And how to I force the axes to be all the same lengths . (i.e, X-axis is 0 to 25 and Y-axis is 0 to 25 for all subplots) ?? Thanks again!

AdityaGreat function!

However, like Hakon, I wanted to be able to use it on the merged plots. Also, I wanted to use the standard Matlab functions as much as possible to draw the plot before applying the resize.

So I wrote a little function:

http://www.mathworks.in/matlabcentral/fileexchange/35464-spaceplots

Maybe that will help.

HakonVery helpful, thank you! This should be standard in matlab, the gap between subplots is such a waste of space.

One thing missing from this function is the ability to merge two subplot areas into a larger plot area for some charts, e.g. subplot(3,2,[1 3]) will make the top left subplot occupy spot 1 and 3 (and so be double size vertically). Can this be added to subplot1 somehow?

trevorGreat Subplot function.

i had similar problems as Dave Van Tol above, with subplot1 giving error messages along the lines of:

??? Error using ==> set

uimenu handle passed to 'CurrentAxes' property - valid axes handle required

Error in ==> subplot1 at 138

set(gcf,'CurrentAxes',H(M));

His suggested fix worked perfectly (include in update?)

Many thanks!

Evgenyueih neyugnNo BodyThis is exactly what I needed, thanks!

Ben KangIt's helpful. But it can not work well with mesh. For example,

subplot1(2, 2);

subplot1(1), imagesc(saliency), axis xy;

subplot1(2), mesh(saliency),

The function imagesc runs well. But mesh display the X-Y plane projection instead of the 3-D view as usual.

Do you have a solution for this?

Thanks!

D FThanks for the file.

Is it possible to create square subplots and still control the gap between plots? In the example below, how would one eliminate the excess space between columns?

%The goal is a 2*2 plot, in which each plot %is square and has equal length axes. %However, the figure has too much horizontal %space between columns.

subplot1(2,2,'Gap',[0.003 0.003])

plot_num = 0;

for i=1:2

for j=1:2

plot_num = plot_num + 1;

subplot1(plot_num);

plot(randn(100,1),randn(100,1),'k.')

axis([-3 3 -3 3])

axis square

end

end

Dave Van TolIt helped me solve my problem.

One thing I have changed... subplot1 assumes that all of the children of the current figure are axes. That assumption is not always true. For example, turning on the data cursor (datacursormode on) adds a handle to the current figure. If that figure is reused, subplot1 mistakes that extra handle as an axes and the result is an error.

The root of the problem seems to be lines 111 and 112, which say:

H = get(gcf,'Children');

Ptot = length(H);

It would be good to eliminate the handles from H that are not axes. I have replaced line 112 with:

Ptot = sum(strcmp(get(H,'type'),'axes'));

That fixes the problem.

Fuu-Ming KaiIt's very good. Save me a lot of time. It will be great if there is more information to talk about how to use it. For example, I just figured out, if you want to add a label in different subplot (e.g. 1st plot), you need to type >> subplot1(1); xlabel('time'). Overall, I like it a lot. Thanks!

Dagang WangIt's good. I like it

Spencer ChenI thought I can add labels to subplot rows and coloumns.... can someone show me how?

Spencer ChenI like the control over the gap size. But this creates axes where I do not intend to place any plots at all. Have to delete those one by one... and in reverse order, so that I do not stuff the internal structure subplot1 has set up.

John D'ErricoNote: If you do as Martin suggests (not a bad idea) only create the array of handles when nargout>0. This way when subplot is called wth no outputs, there will be no spewing of the unwanted handles array to the command line.

Martin LechnerVery good if you add the axes handles as output parameter.

You must change only the following lines:

1: function hAxes = subplot1(M,N,varargin);

139: hAxes = H(M);

172: hAxes(Row,Col) = axes('position',[Xstart,Ystart,Xbox,Ybox]);