2012-05-18 13 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