Formatting Subplots in MATLAB

I am trying to create a series of vertically stacked plots for a scientific publication using the same x-axis. With regards to this task, I have a couple questions.
1) Generating the subplots according to the MATLAB defaults, there is always good bit of space between each individual plot in the group. I am trying to get each plot to sit right on the top of the others, but all of the things I have tried have not worked. From looking around, I believe I need to set height space between the subplots, but I could be wrong. I need tidy way to reduce and/or eliminate the space between the subplots. Current figure and the a figure I am trying the replicate the style found below.
2) Also, does anyone know why the far right y-axis on the bottom plot does not line up with the that of the top plot? Would setting sizes for the plots in the code eliminate this problem? Current code is attached as it is pretty long.

1 Comment

I have resolved my second question, but the first remains unknown.

Sign in to comment.

Answers (2)

Hi you can get the position of bottom subplot using p = get(subplot,'Position'). This is a 4-element vector [left, bottom, width, height]. So you can get the upper edge of the second plot by adding p(2) and p(4). Then start first plot from that position.
Example code:
a = [1:10];
b = [1:10];
figure(1)
h1 = subplot(2,1,1);
scatter(a,b);
ylabel('data1')
set(gca,'XTickLabel',[]);%remove Xaxis tick labels for h1
h2 = subplot(2,1,2);
scatter(a,b);
xlabel('x axis data');
ylabel('data2')
p1 = get(h1, 'Position');
p2 = get(h2, 'Position');
p1(2) = p2(2)+p2(4);%change bottom starting point of h1
set(h1, 'pos', p1);%set h1 position
Try manipulating the position of the second subplot. For example, you can get the current position value of the second subplot as
% get the handle to the subplot
hAxes2 = subplot(2,1,2);
% get the current position
axes2Pos = get(hAxes2, 'Position');
% move the subplot (axes) up
axes2Pos(2) = axes2Pos(2)*2;
% reset the new position
set(hAxes2,'Position',axes2Pos);
The above is just an example. The position array should be a 1x4 with the x coordinate of the bottom left corner of the axes, the y coordinate of the bottom left corner of the axes, the width of the axes, and the height. These values may or may not be normalized. As we want to move this axes up, we just increase the y-coordinate (in this case, by two).
Try the above and see what happens!

3 Comments

Thanks for the advice. I tried and it worked well. I settled on the factor of 1.8-1.85 to avoid have the axis labels on the y-axis and color bar getting jumbled together. However, when I change of the size of the window with the plot, they fall out os proportion as shown in the attached image. Any ideas on how to prevent that from happening? Would setting an image size in the code take care of that problem? If so, do you know how to set plot size? Thanks.
After resizing the window, regenerating plot restores the proportions. If there was a way to keep from not having to do each time. That would be preferred. Thanks.
I can't seem to reproduce that problem with my version of MATLAB (r2014a) so perhaps the code I'm using is somewhat different from yours. Can you post a sample of code and the steps that illustrate the problem?

Sign in to comment.

Asked:

on 14 Sep 2016

Commented:

on 19 Sep 2016

Community Treasure Hunt

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

Start Hunting!