Matlab:当其中一个子图包含一个颜色条时,如何对齐子图的轴? [英] Matlab: How to align the axes of subplots when one of them contains a colorbar?

查看:176
本文介绍了Matlab:当其中一个子图包含一个颜色条时,如何对齐子图的轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最小示例:

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

In this example I'd like to have the lower subplot show the cross-section of peaks along y=0 and the plot ending at the same position as the pcolor subplot, so that the x ticks are on identical positions. In fact, I don't need the duplicate x axis then. So,

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

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)

(仅供参考,学习我可以使用

(FYI I learned I can use the linkaxes command then to have a correct zoom behaviour in a second step)

推荐答案

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

You can just set the width of the second subplot to the width of the first by changing the Position property.

[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标签,然后向上移动第二个以使其接触:

You can then further manipulate your axes properties, for example you can turn of the x-labels on the first plot, and move the second one up so that they touch:

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

这篇关于Matlab:当其中一个子图包含一个颜色条时,如何对齐子图的轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆