2015-12-07 42 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