user4861528
user4861528

Reputation: 467

How to plot several graphs in matlab one below the other with one x-axis?

I have 10 plots of the same data in several time points. I want to show the evolution of the data over time in one matlab figure. I tried to show it using subplot(10,1,i) where i goes from 1 to 10, in order get several plots one beneath the other. However I don't want to have for each plot its own axes. It is superfluous and only takes important space and makes the plots themselves very small in the y direction. I would rather show it as multiple plots one beneath the other with one x-axis. How can I do that?

Upvotes: 2

Views: 3809

Answers (2)

TheodorBecker
TheodorBecker

Reputation: 249

What I usually just do is the following

figure('Position',[x,y,w,b]);
for i = 1:10
    subplot('Position',[x2,y2-i*sc,w2,b2])
    plot here
    xl = get(gca,'XTickLabel');
    set(gca,'XTickLabel','')
    ylabel('bla')
end
set(gca,'XTickLabel',xl)
xlabel('bla2')

in figure you set your desired figure positon and size using x y w and b. By using the argument 'Position',[x2,y2-i*sc,w2,b2] with subplot lets you place the subplots wherever you want. This can be adjusted to reduce white space between plots. That way you can move them very close together. It does require a bit of tuning though, because you have to adjust sc so that plots do not overlap.

Here is an arbitrary example:

figure('Position',[10,10,500,800]);
sc = 0.09;
for i = 1:10
    subplot('Position',[0.1,0.99-i*sc,0.75,0.075])
    plot(1:10)
    xl = get(gca,'XTickLabel');
    set(gca,'XTickLabel','')
    ylabel('bla')
end
set(gca,'XTickLabel',xl)
xlabel('bla2')   

which produces this image:

enter image description here

Upvotes: 2

user5182786
user5182786

Reputation:

You'll have to manually adjust the y-values of each of the 10 different vectors so that they plot stacked on top of each other, by adding some value to each set of y-values.
It's usually easiest just to add multiples of the maximum y-value to each vector, so that the second time iteration is shifted up 1 times max(y), the third iteration is shifted up 2*max(y), and so on, with the guarantee that they won't overlap with each other.
You can also normalize each vector to 1 or 100 or whatever value, and shift the 2nd through 10th vectors by multiples of that value.

Then to make the y-axis conform to the stacked y-values you can submit your own y-axis tick locations and labels by resetting the YTick and YTickLabel values, something like:

set(gca, 'YTick', [0,5,10,15,20,25],'YTickLabel',{'0','5','0','5','0','5'});

which selects the current axes, and applies the input vectors to the YTick and YTickValues properties.

This is also explained in the documentation here: http://www.mathworks.com/help/matlab/ref/axes-properties.html

It's not super convenient but if you're going to be doing this frequently it isn't too much trouble to write a script or function that will automate it, and you might as well get used to manipulating the axes and figure properties if you're going to want more complicated plots like this often.

Upvotes: 0

Related Questions