2017-06-21 91 views
1

我有只在图中两个酒吧一个barh条形图之间的差距,但绘制它们时,它们相距甚远很:蟒蛇matplotlib BARH减少吧

import numpy as np 
import matplotlib.pyplot as plt 

labels = ('Out', 'In') 
bar_values = [5, 10] 
num_items = len(labels) 
width = 0.25 
ind = np.arange(num_items) 
bar_width = 0.1 

fig = plt.figure() 

ax = fig.add_subplot(111) 

barlist = ax.barh(ind, 
       bar_values, 
       bar_width, 
       align='edge', 
       color='green') 

barlist[0].set_color('mediumseagreen') 
barlist[1].set_color('orangered') 

ax.set_yticks(ind) 
ax.set_yticklabels(labels) 
ax.invert_yaxis() # labels read top-to-bottom 
ax.set_xlabel('Total') 

plt.show() 

是否有把杆的方式走得更近?我已经尝试指定图形的大小,但这只会减小整体大小并且不会影响间隙大小...

回答

2

您可以通过设置bar_width = 0.6或类似的方法来增加条的宽度值,也可以减少该图的y范围内,例如:

barlist = ax.barh([0.1, 0.3], 
       bar_values, 
       bar_width, 
       align='edge', 
       color='green') 
ax.set_yticks([0.1, 0.3]) 
ax.set_yticklabels(labels) 

两者均应增加条的宽度相对于条之间的距离。 Reduced y range