Forcing two subplots to have the same width

13 vues (au cours des 30 derniers jours)
David Aronstein
David Aronstein le 17 Août 2014
Commenté : Star Strider le 18 Août 2014
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?

Réponse acceptée

Star Strider
Star Strider le 17 Août 2014
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 commentaires
David Aronstein
David Aronstein le 18 Août 2014
Thank you!
Star Strider
Star Strider le 18 Août 2014
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by