垂直子图的单个配色条 [英] Single Colorbar for Vertical Subplots

查看:51
本文介绍了垂直子图的单个配色条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使下面的MATLAB图具有沿两个子图延伸的单个颜色条.

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

注意:这与

  figure('color','white','DefaultAxesFontSize',fontSize,'pos',posVec)ax(1)=子图2(2,1,1);pcolor(x2d,t2d,dataMat1)阴影插入ylim([0 10])xlim([-0.3 0.3])xticklabels({})设置(gca,'clim',[-20 0])颜色图(flipud(灰色))设置(gca,'layer','top')轴ijax(2)=子图2(2,1,2);pcolor(x2d,t2d,dataMat2);xlabel('x')ylabel('y')阴影插入ylim([0 10])xlim([-0.3 0.3])设置(gca,'气候',[-20 0])yticklabels({})cbar =颜色条;cbar.Label.String ='标准化单位';颜色图(flipud(灰色))设置(gca,'layer','top')轴ij 

第2步.保存两个子图的位置矢量和颜色栏.

  pos1 = ax(1).Position;%位置向量= [x y宽度高度]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)时,相应的轴不再相应缩放.从

I would like to make the following MATLAB plot have a single colorbar that extends along both subplots.

Something like this (done manually with the figure editor):

Note: This is different from the question asked here.

Thanks!

解决方案

I finally figured out a solution. The colorbar can be manually positioned in code but I wanted to keep everything the original spacing. My final solution is outlined below.

Step 1. Create the plot with a single colorbar on the bottom subplot.

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

Step 2. Save the position vectors of the two subplots and the colorbar.

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

Step 3. Update the position of the colorbar to extend to the top of the top subplot.

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

Step 4. Update the width of the top subplot to accommodate the colorbar.

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

Step 5. Update the width of the bottom subplot to accommodate the colorbar.

ax(2).Position = pos2;

Wait, I thought the bottom subplot already accommodated the colorbar? Actually, when setting the position of the colorbar manually (step 3), the corresponding axis no longer scales accordingly. From the documentation:

If you specify the Position property, then MATLAB changes the Location property to 'manual'. The associated axes does not resize to accommodate the colorbar when the Location property is 'manual'.

The final result:

这篇关于垂直子图的单个配色条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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