Stuart’s MATLAB Videos

Watch and Learn

Advanced MATLAB: Subplots 15

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.

15 CommentsOldest to Newest

Doug replied on : 1 of 15

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


Antunes replied on : 2 of 15


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


Doug replied on : 3 of 15


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.


Bassam Awad replied on : 4 of 15


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 15


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.


Dimitrij replied on : 6 of 15

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 15

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



jitendra replied on : 9 of 15

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


wrongname replied on : 11 of 15

the subplot function is divide the current window into m*n parts. here m is the number of rows and the n is number of column. p is the position where we will plot the graph.
if there graph is already exists the blow command is delete the pane and create a new pane of graph.

Mindy replied on : 12 of 15

Hi Doug, I saw couple of your posts on Internet, wondering if you can help me.

How do I get YData in one subplot? When I do get(gca,’children’), it only returns one value although there are several subplots.
when i do get(get(gca,’Children’),’ydata’); it doesn’t seem like ydata of all subplots, i don’t know how to read the return of this line.
I also have tried h(1) = subplot(….), then get(h(1),’YData’)), didn’t seem working either.
Anyway, how do I get Ydata of a subplot.

Also, if I set all subplots in a figure with buttondownfunction, what’s the best way to do this? currently i have a for loop as for i = 1:length(#of subplot)
end. This is not a good way to do it at all, is it? what is the faster way to do this?

One more question is that is there a way to add uicontrol,like a text box, within a subplot(rather than the entire figure? If so, how should I do it. I wasn’t able to do it for individual subplot.

Thank you

Doug replied on : 13 of 15


Axes do not have YData, lines do. To get the Ydata, you need the handles of the children of the axes (the lines). Do you mean the YLimits of the axes?


Inquisitor X replied on : 14 of 15

Hi Doug, is there a generalised way for numbering subplots (ie a(i) a(ii) a(iii), etc.)? I tried labelling a set of 4 by 3 subplots on the upper left corner of each graph. I used the text function and the maximum y-limit to work out the position of the label, ie.

h1 = subplot(4,3,1); plot(x,y);
lim_y12 = get(h1, ‘YLim’);

text(0, lim_y1(2)-1, [‘ ‘ char(1+96) ‘(i)’];,…

By all means, it worked out okay, but I’m not completely satisfied with the method. The scales from each column are varied from one and another by different order of magnitudes and so the labels would end up scattering about along the horizontal axis (mildly). Is there a way to set the position of the labelled text relative to the graph area instead of using the plot coordinates? I simply want number each graph at exactly the same place. Any helps and suggestions would be greatly appreciated.

Best wishes,

Doug replied on : 15 of 15

When you put down text objects, they have a ‘units’ field. Turn the units to ‘normalized’. Then the text is relative to axes, location goes from [0 1].


Add A Comment

Your email address will not be published. Required fields are marked *

What is 6 + 10 ?

Preview: hide