File Exchange Pick of the Week

Our best user submissions

Advanced MATLAB: Subplots 20

Posted by Doug Hull,

The distinction between MATLAB Basics and Advanced MATLAB is a somewhat arbitrary distinction. The heuristic I use is “If I knew it before I came to The MathWorks, it is basic.” It is as good of a rule as any.

Some might argue this is a more basic post, but the inclusion of the gotcha and the use of property value pairs made this an advanced topic in my mind.

This short video shows how to put multiple subplots on a figure. Axes of different sizes can be laid out. The use of the position property is covered. A common gotcha is shown also where one axis overlapping another will delete the first.

Video Content

lazy.jpg

Note

Comments are closed.

20 CommentsOldest to Newest

Doug replied on : 1 of 20

It was just pointed out that there is a typo at two minutes into the video. The title of the subplot on the right should be:

h=subplot(‘position’, [0.6 0.1 0.3 0.8])

Sorry!
Doug

Antunes replied on : 2 of 20

Hi,

Could you please give code to plot say 2 subplots side-by-side with the axes obeying the golden ratio?

thanks
a.

Doug replied on : 3 of 20

Antunes,

You should be able to do that with the ‘position’ property value pair. Just get the length and width in ratio and then position appropriately.

Doug

Bassam Awad replied on : 4 of 20

Hello,

I am trying to plot data in 393 plots with each figure include 25 plots. Any help is really appreciated.

Doug replied on : 5 of 20

Bassam,

That seems a little extreme to have 393 plot windows open. Having more than a dozen windows open in a Windows OS seems difficult to manage. I would try to think of a more effective way to display your data.

Maybe a GUI that allows you to select the graph you are interested in would be better. Maybe an animation is more what you need.

Doug

Dimitrij replied on : 6 of 20

thanks for the hint: I finally understood the “position” command applied to subplot, and that dimensions are given in percentage of figure width and height.

Fikri replied on : 7 of 20

Hi Doug,

got a simple question here, I’m making an interface for an image compression project. I needed to generate several subplots for the histogram, but here lies the problem. After generating all the subplots (which I put inside a panel, not axes) I simply can’t remove/clear/reset those subplots, which I need to do to make my ‘Clear’ pushbutton works as it is assigned to do.

a quick reply would be nice..

regards

Fikri

jitendra replied on : 9 of 20

Hai Doug

I need your help in plotting a time-series data set and in the form of sub-plots.My domain size is 362 X 181 and like that there are 12 data sets.I need to plot all these 12 data sets of same dimension and animate later.
I need to make a script so that,it is operational.

I strucked from last four days to do this.
Thanking you in advance

regards
jitendra

S. G. Poss replied on : 11 of 20

With this nice demo, I now know what my problem is (something is overlaping), causing the first title not to be displayed and all titles moved up one histogram, with the last without a title.

The question is: how do you solve this problem?

I’ve shortened the titles as much as possible, but to no avail?

Suggestions?

The property section of the subplots is not thorough in the documentation and I note that the relative order of property statements can also produce bilwildering results.

Looks like an area that needs some work or at least more documentation to allow workers to work around such problems.

Thank you.

Doug replied on : 12 of 20

S.G.

I am not sure what you are talking about. It is much better for us to be able to help if you can show the shortest code snippet that will reproduce the behavior.

Thanks,
Doug

Rashmish replied on : 13 of 20

Hi Doug,

I have a problem in Matlab, and I hope you can help me with it. I am solving a Differential Equation in Matlab using Matlab’s own functions, for different values of some parameters. For some values of the parameter, it takes too long and I stop the program by a Ctrl-C manually, and then change the parameter.
Is there a way to automate this? i.e., the program should itself figure out that it took more than say some specific amount of time and so it should move to the next parameter.
I hope you got the question.
Your comments would be greatly appreciated !
Thanks in advance.

Rashmish

Steve L replied on : 14 of 20

Rashmish,

There’s no “timeout” capability for the ODE solvers. However, it sounds like the stiffness of your ODEs may depend on the parameters.
You should try switching your program to use a stiffer solver to try to reduce the amount of time it takes to solve the ODE when it’s stiff. There’s a table in the function reference page for the ODE solvers that describes which solvers are stiff solvers and which aren’t.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/ode113.html?BB=1

If you’re currently using ODE45, try ODE15S or ODE23S.

Rashmish replied on : 15 of 20

I finished my work by the manual bull-work, but it wd still be helpful to know if there is any way at all. I am solving a BVP; I don’t know if your comments on stiffness would still help. Do you have some other suggestions about my problem?

Thanks

Don replied on : 16 of 20

Can anyone tell me how to create a subplot of 4-6 plots using pre-existing figure? I have the figures in my current directory ready to go but I can’t get them (copy, import???) into a subplot so they are all together. Any help would be great!! Thank you

Don

Doug replied on : 17 of 20

@Don,

You can reparent the axes that have the plots. The axes have a property, ‘parent’. set that property to the new figure and then change the position to whatever you want.

Doug

fabian replied on : 18 of 20

hi doug,

is it possible to create subplots within a subplot?
as an example, this is what i mean:

for i=1:25
    subplot(5,5,i)   %this would define the "big" plot
   
    subplot(2,1,1)  % this would be a subplot within the bigger subplot
    imagesc(x(i))
    subplot(2,1,2)
    imagesc(y(i)) 
end

each subplot-subplot would consists of two heat maps (image function). if that is not possible, is there a way to code an offset in an imagesc plot (i.e. rows separated from each other)?

thanks

Doug replied on : 19 of 20

@Fabian

Do you mean there would an axis within an axis, sort of like picture in picture on a television?

When you do subplot, each can be of a different size:


subplot(2,1,1); 
subplot(2,2,3);
subplot(2,2,4);

If you use subplot and they overlay each other, as a feature the old one will be deleted. The axis command with position property does not though.

Sara replied on : 20 of 20

Hello,

I have a problem with the Position of the x-axis. I have 3 subplots set vertically in 3 rows and 1 column (subplot(3,1,1) etc.) and the problem is that the title of the subplot (“b)”, for exemple) covers the xlabel of the subplot above. Thus, I move to the right the xlabel by the code
xla2=xlabel(‘time [s]’,’Interpreter’,’Latex’,’fontsize’,12);
pxla = get(xla2,’pos’);
pxla= pxla – [-0.9 0.15 0].*pxla;
set(xla2,’pos’, pxla);
but, when I save the figure as a .fig, this change of the xlabel position is not save. If I open the .fig saved, the xlabel is centered and covered by the title of the subplot located under. Any ideas how to solve this problem?
I really need to save this position in a .fig (if I save in .eps format, the position is correct…).
Thank you