2015-12-07 41 views
0

我正在繪製帶有matplotlib的水平條形圖,標籤在y軸上。用matplotlib完全在條形圖上顯示標籤

import numpy as np 
import matplotlib.pyplot as plt 

labels = ["Something really long here", "normal"] 
values = [10, 5] 
plt.barh(range(len(labels)), values) 
plt.yticks(np.arange(len(labels)) + .5, labels, rotation='horizontal') 
plt.show() 

但是我不滿意的結果:plt.show()不完全顯示的標籤,就像這樣: enter image description here

是否有任何選項來實現這一目標(即不僅具有「y long here」顯示)?

回答

1

您可以使用tight_layout功能,防止窗口切標籤:

import numpy as np 
import matplotlib.pyplot as plt 

labels = ["Something really long here", "normal"] 
values = [10, 5] 
plt.barh(range(len(labels)), values) 
plt.yticks(np.arange(len(labels)) + .5, labels, rotation='horizontal') 
plt.tight_layout() 
plt.show() 

enter image description here