gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:13:55 25 4
gpt4 key购买 nike

最小的例子:

[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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com