Vertically-stacked subplots cannot be made the same width and to be vertically aligned
3 ビュー (過去 30 日間)
古いコメントを表示
Michael Worthington
2022 年 10 月 25 日
コメント済み: Michael Worthington
2022 年 10 月 26 日
I have two subplots: one an image, and one a line plot. I am trying to make it so the two subplots have the same width and are vertically aligned (i.e., have the same distance from the left edge of the window). My understanding of the figure property "Position" is that if you have a figure h, then h.Position returns a vector of the form [left right width height]. I need to make sure then that the "left" and "width" values for each subplot are the same. This is based on the information at https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html . I am attempting this with the following code:
clc
clear
close all
h1 = subplot(2, 1, 1); % image
imagesc(rand(15, 25))
daspect([1 1 1])
colorbar('southoutside')
set(gca,'xtick',[])
set(gca,'ytick',[])
h1.Units = 'centimeters';
h2 = subplot(2, 1, 2); % line plot
plot(1:25, 1:25)
h2.Units = 'centimeters'; % making sure both subplots have the same units
h2.Position(1) = h1.Position(1); % position of left edge of plot to left
% edge of window should be the same
h2.Position(3) = h1.Position(3); % both subplots should be the same width
h1.Position
h2.Position
The outputs of the last two lines give:
h1.Position = [1.9262 6.4879 11.4829 3.7912]
h2.Position = [1.9262 1.2224 11.4829 3.7912]
The 1st and 3rd value in the Positions of each subplots are the same, so I would expect the subplots to have the same width and to be vertically aligned. However, this is the plot produced:

The subplots are not the same width and the left edges are not aligned. The top subplot should be wider, or at least shifted to the left. I am following the same procedure as I have done previously to succesfully do this, but it is now failing. What am I doing incorrectly? What am I missing?
0 件のコメント
採用された回答
Matt J
2022 年 10 月 25 日
Something to do with daspect(). Omitting it gives the intended edge alignment.
Units='normalized';
h1 = subplot(2, 1, 1); % image
imagesc(h1,rand(15, 25))
%daspect([1 1 1])
colorbar('southoutside')
set(gca,'xtick',[])
set(gca,'ytick',[])
h1.Units = Units;
h2 = subplot(2, 1, 2); % line plot
plot(h2, 1:25, 1:25)
h2.Units = Units; % making sure both subplots have the same units
h2.Position([1,3]) = h1.Position([1,3]);
drawnow
2 件のコメント
Matt J
2022 年 10 月 25 日
This seems to compensate for whatever daspect is doing:
Units='normalized';
h1 = subplot(2, 1, 1); % image
imagesc(h1,rand(15, 25))
daspect([1 1 1])
colorbar('southoutside')
set(gca,'xtick',[])
set(gca,'ytick',[])
h1.Units = Units;
h2 = subplot(2, 1, 2); % line plot
plot(h2, 1:25, 1:25)
h2.Units = Units; % making sure both subplots have the same units
h2.Position([1,3]) = h1.Position([1,3])+[0.25,-0.5];
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Axis Labels についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

