用于垂直子图的单色条

问题描述 投票:2回答:1

我想让下面的MATLAB图有一个沿着两个子图延伸的单一颜色条。

enter image description here

这样的事情(使用图形编辑器手动完成):

enter image description here

注意:这与here提出的问题不同。

谢谢!

matlab matlab-figure
1个回答
4
投票

我终于找到了解决方案。可以在代码中手动定位颜色条,但我想保留原始间距的所有内容。我的最终解决方案概述如下。

步骤1.在底部子图上使用单个颜色条创建绘图。

enter image description here

figure('color', 'white', 'DefaultAxesFontSize', fontSize, 'pos', posVec)
ax(1) = subplot2(2,1,1);
pcolor(x2d, t2d, dataMat1)
shading interp
ylim([0 10])
xlim([-0.3 0.3])
xticklabels({})
set(gca, 'clim', [-20 0])
colormap(flipud(gray))
set(gca,'layer','top')
axis ij
ax(2) = subplot2(2,1,2);
pcolor(x2d, t2d, dataMat2);
xlabel('x')
ylabel('y')
shading interp
ylim([0 10])
xlim([-0.3 0.3])
set(gca, 'clim', [-20 0])
yticklabels({})
cbar = colorbar;
cbar.Label.String = 'Normalized Unit';
colormap(flipud(gray))
set(gca,'layer','top')
axis ij

步骤2.保存两个子图和颜色条的位置矢量。

pos1 = ax(1).Position; % Position vector = [x y width height]
pos2 = ax(2).Position;
pos3 = cbar.Position;

步骤3.更新颜色条的位置以延伸到顶部子图的顶部。

cbar.Position = [pos3(1:3) (pos1(2)-pos3(2))+pos1(4)];

步骤4.更新顶部子图的宽度以容纳颜色条。

ax(1).Position = [pos1(1) pos1(2) pos2(3) pos1(4)];

步骤5.更新底部子图的宽度以容纳颜色条。

ax(2).Position = pos2;

等等,我认为底部子图已经容纳了颜色条?实际上,当手动设置彩条的位置时(步骤3),相应的轴不再相应地缩放。来自documentation

如果指定Position属性,则MATLAB将Location属性更改为“manual”。当Location属性为“manual”时,关联的轴不会调整大小以容纳颜色条。

最终结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.