重新定位颜色条或子图

时间:2013-04-17 15:41:57

标签: matlab matlab-figure

我正在尝试将颜色条放在一系列四个子图的右侧。 我正在使用此代码,但条形图与最后一个图像相交,如图problem

所示
ax(1)=subplot(1,3,1);imagesc(stats.mean,[0 1]);colormap(jet(256)); title('mean');
ax(2)=subplot(1,3,2);imagesc(stats.median,[0 1]);colormap(jet(256)); title('median');
ax(3)=subplot(1,3,3);imagesc(stats.std,[0 1]);colormap(jet(256)); title('std');
h=colorbar;
set(h, 'Position', [.8314 .11 .0581 .8150]);
for i=1:3
pos=get(ax(i), 'Position');
set(ax(i), 'Position', [pos(1) pos(2) 0.85*pos(3) pos(4)]);
end;

1 个答案:

答案 0 :(得分:4)

我建议采用不同的方法。

假设您正在绘制以下内容:

ax(1) = subplot(1,3,1);imagesc(rand(100,1),[0 1]);
ax(2) = subplot(1,3,2);imagesc(rand(100,1),[0 1]);
ax(3) = subplot(1,3,3);imagesc(rand(100,1),[0 1]);

我建议只需重置受colorbar影响的第三个子图的尺寸,并拉伸图形以包含添加的颜色条。

% Get positions of all the subplot
posa = get(ax,'position');
h    = colorbar;

% Reset ax(3) position to before colorbar
set(ax(3),'position',posa{3})

% Set everything to units pixels (avoids dynamic reposition)
set([ax h],'units','pix')

% Widen figure by a factor of 1.1 (tweak it for needs)
posf = get(gcf,'position');
set(gcf,'position',[posf(1:2) posf(3)*1.1 posf(4)])

结果

enter image description here