Formatting Subplots in MATLAB

62 ビュー (過去 30 日間)
AmericanExpat26
AmericanExpat26 2016 年 9 月 14 日
コメント済み: Geoff Hayes 2016 年 9 月 19 日
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 件のコメント
AmericanExpat26
AmericanExpat26 2016 年 9 月 14 日
I have resolved my second question, but the first remains unknown.

サインインしてコメントする。

回答 (2 件)

Alvi Newaz
Alvi Newaz 2016 年 9 月 14 日
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

Geoff Hayes
Geoff Hayes 2016 年 9 月 14 日
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 件のコメント
AmericanExpat26
AmericanExpat26 2016 年 9 月 14 日
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.
Geoff Hayes
Geoff Hayes 2016 年 9 月 19 日
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?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by