2015-06-16 65 views
1

我試圖繪製一個水平堆積的條形圖,但在頂部和底部獲得令人討厭的大邊緣。我想擺脫那個或控制大小。在pyplot中操縱頂部和底部邊距水平堆積條形圖(barh)

下面是一個例子代碼和無花果:

from random import random 
Y = ['A', 'B', 'C', 'D', 'E','F','G','H','I','J', 'K'] 
y_pos = np.arange(len(Y)) 
data = [(r, 1-r) for r in [random() for i in range(len(Y))]] 
print data 
a,b = zip(*data) 

fig = plt.figure(figsize=(8,16)) 
ax = fig.add_subplot(111) 
ax.barh(y_pos, a,color='b',align='center') 
ax.barh(y_pos, b,left=a,color='r',align='center') 
ax.set_yticks(y_pos) 
ax.set_yticklabels(Y, size=16) 
ax.set_xlabel('X label', size=20) 

plt.xlim(0,1) #removing the extra x on the right side 

如果要重新創建確切的數字,採樣創建附圖中( '數據')的順序:

[(0.2180251581652276,0.7819748418347724 ),(0.20639063565084814,0.7936093643491519),(0.5402540115461549,0.45974598845384507),(0.39283183355790896,0.607168166442091),(0.5082547993681953,0.4917452006318047),(0.02489004061858613,0.9751099593814139),(0.25902114354397665,0.7409788564560233),(0.12032683729286475,0.8796731627071352),(0.41578415717252204,0.584215842827478), (0.5860546624151288,0.4 139453375848712),(0.20982523758445237,0.7901747624155476)]

enter image description here

我想對 'K' 的頂部和x軸和 'A' 之間的更小的間隙。 如果我改變了16:

fig = plt.figure(figsize=(8,16)) 

10的一切收縮,但空間依然比較大。

任何想法? 謝謝!

+0

至於樣式,我導入seaborn和font ='family':'fantasy','weight':'normal'} matplotlib.rc('font',** font) – ScienceFriction

回答

3

您可能要將此行添加到您的腳本的末尾:

plt.ylim(min(y_pos)-1, max(y_pos)+1) 

這將利潤率降低到半一欄的寬度。

+0

謝謝!像魅力一樣工作。在發佈之前,我正在玩ylim,但無法完成。 – ScienceFriction