首页 文章

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

提问于
浏览
12

最小的例子:

[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轴 . 所以,

如何重新缩放下部子图,使右边界限与上边界图部分的右边界相匹配? (最好是这样可以在不破坏对齐的情况下打开/关闭彩条)

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

1 回答

  • 15

    您可以通过更改 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

相关问题