Forcing two subplots to have the same width

조회 수: 14 (최근 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 CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by