Python 条形图重叠

标签 python numpy matplotlib bar-chart

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

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()

生成的条形图如下所示。 enter image description here

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

最佳答案

您是否尝试过设置width = 0.15?我不知道总数是否必须低于 1。

执行此操作时,条形图绘制得很好:

enter image description here

关于Python 条形图重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31450563/

相关文章:

python - Matplotlib 具有转换值的第二个 x 轴

Python 多行 JSON 和变量

python - 与进程池执行程序一起运行多个进程的 Tornado 并发错误

pandas - 如何在 Python 中查找不包括周末和某些假期的两个日期之间的小时数?营业时间套餐

python - 使用 pandas、numpy 或其他方法将 numpy 数组连接到两个数组

python - 如何在 sns countplot 中仅堆叠一根柱

python - Django request.post 无可用数据

python - 从 Django View 中更改 css 样式

python - statsmodels AR模型答案错误

python - Matplotlib 补丁大小(以点为单位)