4.16667

4.2 | 13 ratings Rate this file 56 Downloads (last 30 days) File Size: 5.99 KB File ID: #9694

An improved subplot function

by Eran Ofek

 

19 Jan 2006 (Updated 20 Jan 2006)

An improved subplot function

| Watch this File

File Information
Description

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));
...

MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (14)
24 Jan 2006 Martin Lechner

Very 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]);

24 Jan 2006 John D'Errico

Note: 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.

17 Nov 2006 Spencer Chen

I 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.

17 Nov 2006 Spencer Chen

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

15 Feb 2007 Dagang Wang

It's good. I like it

05 Oct 2007 Fuu-Ming Kai

It'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!

15 Feb 2008 Dave Van Tol

It 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.

13 Apr 2008 D F

Thanks 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

21 May 2008 Ben Kang

It'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!

13 Jun 2008 No Body

This is exactly what I needed, thanks!

07 May 2010 ueih neyugn  
12 Nov 2010 Evgeny  
17 Nov 2010 trevor

Great 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!

03 May 2011 Hakon

Very 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?

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
annotation Eran Ofek 22 Oct 2008 08:12:51
customization Eran Ofek 22 Oct 2008 08:12:51
subplot margin Eran Ofek 22 Oct 2008 08:12:51
subplot function Eran Ofek 22 Oct 2008 08:12:51
rows Eran Ofek 22 Oct 2008 08:12:51
plot Eran Ofek 22 Oct 2008 08:12:51
graphics Eran Ofek 22 Oct 2008 08:12:51
columns Eran Ofek 22 Oct 2008 08:12:51
subplot margin Alan B 11 Apr 2011 23:39:34
subplot margin Pat 26 Sep 2011 11:12:22

Contact us at files@mathworks.com