Matlab:当其中一个包含颜色条时如何对齐子图的轴?

标签 matlab plot axes

最小的例子:

[x,y,z] = peaks(50);
figure;
subplot(5,1,1:4);
pcolor(x,y,z);
shading flat;
colorbar;
subplot(5,1,5);
plot(x(end/2,:), z(end/2,:));

output

在这个例子中,我想让下面的子图显示沿 y=0 的峰的横截面,并且该图在与 pcolor 子图相同的位置结束,以便 x 刻度位于相同的位置。事实上,那时我不需要重复的 x 轴。所以,

How to rescale the lower subplot such that the right limit matches the right limit of the upper one's plot part? (preferably such that the colorbar can be switched on/off without destroying that alignment)

(仅供引用 learned 我可以使用 linkaxes 命令然后在第二步中获得正确的缩放行为)

最佳答案

您可以通过更改 Position 属性将第二个子图的宽度设置为第一个子图的宽度。

[x,y,z] = peaks(50);
figure;
ah1 = subplot(5,1,1:4); %# capture handle of first axes
pcolor(x,y,z);
shading flat;
colorbar;
ah2 = subplot(5,1,5); %# capture handle of second axes
plot(x(end/2,:), z(end/2,:));

%# find current position [x,y,width,height]
pos2 = get(ah2,'Position');
pos1 = get(ah1,'Position');

%# set width of second axes equal to first
pos2(3) = pos1(3);
set(ah2,'Position',pos2)

然后您可以进一步操作坐标轴属性,例如,您可以关闭第一个图上的 x 标签,然后向上移动第二个图,使它们接触:

set(ah1,'XTickLabel','')
pos2(2) = pos1(2) - pos2(4);
set(ah2,'Position',pos2)

enter image description here

关于Matlab:当其中一个包含颜色条时如何对齐子图的轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5259853/

相关文章:

r - 如何将 x 和 y 轴的截距放置在 (0 , 0) 并将 x 和 y 轴延伸到绘图的边缘

matlab - 使用 bsxfun 将一个矩阵的所有列乘以另一个矩阵

matlab - 如何识别 MATLAB 中向量中至少 10 个相邻 '1' 的第一次出现?

R 绘图 : Using italics and a variable in a title

matlab - Matlab 绘图的自定义标记

sql - 从 Postgresql 表中绘制表和关系

matlab - 在 MATLAB 中自动将图形写入文件

algorithm - 使用霍夫变换进行椭圆检测

python - Matplotlib——双胞胎 : how to align values of two x-axes in one plot?

python - add_axes 和 add_subplot 有什么区别?