Forcing two subplots to have the same width
조회 수: 14 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!