最小示例:
[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轴。所以,
如何重新缩放下部子图,以使右边界与上部子图的右边界匹配? (最好是可以在不破坏对齐的情况下打开/关闭颜色条)
(仅供参考,我
知道我可以使用
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)
更多&回答...