Python 条形图重叠

Python Bar charts are overlapping

我使用 python 的 numpy 和 matplotlib 绘制了一个条形图,除了分组条形之间的 space 之外,一切都很好。分组条形图重叠,在实际情况下它们之间应该有足够的 space。这是实现的代码。

import numpy as np
import matplotlib.pyplot as pyplot

N = 5
shd0hrs = (195, 198, 195, 193, 270)
shd1hrs = (159, 160, 148, 155, 208)
shd2hrs=(55, 49, 48, 39, 56)
shd6hrs=(0, 0, 0, 0, 0)
shd7ahrs=(21, 20, 18, 17, 26)
shd8hrs=(43, 38, 35, 31, 42)

width = 0.30       # the width of the bars

ind = np.arange(N)  # the x locations for the groups


fig = pyplot.figure()
ax = fig.add_subplot(111)

rectshd0= ax.bar(ind,shd0hrs,width,color='r')
rectshd1= ax.bar(ind+width,shd1hrs,width,color='g')
rectshd2= ax.bar(ind+2*width,shd2hrs,width,color='b')
rectshd6= ax.bar(ind+3*width,shd6hrs,width,color='y')
rectshd7a= ax.bar(ind+4*width,shd7ahrs,width,color='m')
rectshd8= ax.bar(ind+5*width,shd8hrs,width,color='c')

# add some text for labels, title and axes ticks
ax.set_ylabel('GlareHrs/yr')
ax.set_title('West Orientation')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('8am-10am', '10am-12pm', '12pm-2pm', '2pm-4pm', '4pm-6pm') )

ax.legend((rectshd0[0],rectshd1[0],rectshd2[0],rectshd6[0],rectshd7a[0],rectshd8[0]),('E1-geo0','E1-geo1','E1-geo2','E1-geo6','E1-geo7a','E1-geo8'))

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rectshd0)
autolabel(rectshd1)
autolabel(rectshd2)
autolabel(rectshd6)
autolabel(rectshd7a)
autolabel(rectshd8)

pyplot.show()

生成的条形图如下所示。

我玩过 np.arange() 但没有用,我不知道我是否遗漏了什么。我也想知道我是否可以在图表中调出更多美观的颜色。非常感谢任何帮助。

你试过设置width = 0.15吗?不知道加起来是不是一定要小于1

这样做时,条形图绘制得很好: