Forcing two subplots to have the same width

15 ビュー (過去 30 日間)
David Aronstein
David Aronstein 2014 年 8 月 17 日
コメント済み: Star Strider 2014 年 8 月 18 日
In the simple script below, I create two matrices, having different numbers of rows but the same number of columns:
m1 = rand(64, 512);
m2 = rand(128, 512);
h=figure;
subplot(2,1,1);
imagesc(m1);
axis equal;
xlim([1, 512]);
ylim([1, 64]);
title('64x512');
subplot(2,1,2);
imagesc(m2);
axis equal;
xlim([1, 512]);
ylim([1, 128]);
title('128x512');
set(h, 'Position', [100, 100, 800, 300]);
In the last line, when the figure size/aspect ratio is changed, the top image is rendered wider in the figure than the bottom image.
How can I set things up so that the two images are drawn with the same WIDTH, regardless of how the figure size/position is adjusted, while preserving the "axis equal" -- that the bottom figure is drawn with 2x the height of the top figure?

採用された回答

Star Strider
Star Strider 2014 年 8 月 17 日
You have to increase the figure height to accommodate the change, then set the height of subplot(2,1,2) to 2x the height of subplot(2,1,1):
m1 = rand(64, 512);
m2 = rand(128, 512);
h=figure;
set(h, 'Position', [100, 100, 800, 600]); % <— ‘Height’ Increased
subplot(2,1,1);
imagesc(m1);
axis equal;
xlim([1, 512]);
ylim([1, 64]);
title('64x512');
hsp1 = get(gca, 'Position') % Get 'Position' for (2,1,1)
subplot(2,1,2);
imagesc(m2);
axis equal;
xlim([1, 512]);
ylim([1, 128]);
title('128x512');
hsp2 = get(gca, 'Position') % Get 'Position' for (2,1,2)
set(gca, 'Position', [hsp2(1:3) 2*hsp1(4)]) % Use 2*(2,1,1) Height for (2,1,2)
  2 件のコメント
David Aronstein
David Aronstein 2014 年 8 月 18 日
Thank you!
Star Strider
Star Strider 2014 年 8 月 18 日
My pleasure!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by