最小的例子:
[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,:));
在这个例子中,我想让下面的子图显示沿 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)
最佳答案
您可以通过更改 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)
关于Matlab:当其中一个包含颜色条时如何对齐子图的轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5259853/