2012-05-18 14 views
10

让我们说我们有一个1×2的插曲,我们绘制一些图形里面如下:如何保持插曲把一个彩条后无变化的尺寸

subplot(1,2,1) 
surf(peaks(20)) 

subplot(1,2,2) 
surf(peaks(20)) 

enter image description here

然后我们想要把一个彩条:

colorbar 

enter image description here

我不不想让结果中出现正确的数字。我们如何才能将彩条放在一排子图中最右边的图上,并保持它们的大小不变?

注意:实际上,我需要它来绘制颜色条常见的图像,我想把它放在右边。为了简单,我使用了这个玩具示例。

回答

13

您可以提取第一个绘图的位置并在第二个绘图上使用。重新缩放时,MATLAB自动将颜色条移动到右侧。

f1=figure(1);clf; 
s1=subplot(1,2,1); 
surf(peaks(20)); 

s2=subplot(1,2,2); 
surf(peaks(20)); 
hb = colorbar('location','eastoutside'); 

%% # Solution: 
s1Pos = get(s1,'position'); 
s2Pos = get(s2,'position'); 
s2Pos(3:4) = [s1Pos(3:4)]; 
set(s2,'position',s2Pos); 



%% # Alternative method. Brute force placement 
set(s1,'Units','normalized', 'position', [0.1 0.2 0.3 0.6]); 
set(s2,'Units','normalized', 'position', [0.5 0.2 0.3 0.6]); 
set(hb,'Units','normalized', 'position', [0.9 0.2 0.05 0.6]); 

enter image description here

+0

太棒了!自动解决方案更好,但强力让我减少图之间的间距,使其等于右图和彩条之间的间距。感谢您提供两种不错的方式。 – petrichor

1

这正是我一直在寻找。在执行Vidar's automatic solution后,我想出了一个简化。在添加颜色条之前获取最右侧轴的位置,然后仅将挤压位置重置为原始位置:

f1=figure(1);clf; 
s1=subplot(1,2,1); 
surf(peaks(20)); 

s2=subplot(1,2,2); 
surf(peaks(20)); 
s2Pos = get(s2,'position'); 

hb = colorbar('location','eastoutside'); 
set(s2,'position',s2Pos); 
+0

我试过了,但彩条的标签似乎超出了情节。 – petrichor