MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   Matlab:当其中一个子图包含一个颜色条时,如何对齐子图的轴? (https://www.labfans.com/bbs/showthread.php?t=26563)

poster 2019-12-14 20:13

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,:)); [IMG]https://i.stack.imgur.com/rWOsi.png[/IMG]

在此示例中,我希望下部子图显示沿y = 0的峰的横截面,并且该图在与pcolor子图相同的位置结束,以便x刻度位于相同的位置。实际上,我不需要重复的x轴。所以,
[INDENT]如何重新缩放下部子图,以使右边界与上部子图的右边界匹配? (最好是可以在不破坏对齐的情况下打开/关闭颜色条)

[/INDENT](仅供参考,我[URL="https://stackoverflow.com/q/5023085/321973"]知道[/URL]我可以使用[URL="http://www.mathworks.com/help/techdoc/ref/linkaxes.html"]linkaxes[/URL]命令,然后在[URL="http://www.mathworks.com/help/techdoc/ref/linkaxes.html"]第二步[/URL]中具有正确的缩放行为)



[B]回答:[/B]

您可以通过更改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) [IMG]https://i.stack.imgur.com/EwDOO.png[/IMG]



[url=https://stackoverflow.com/questions/5259853]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 09:15

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.