【问题标题】:Python Bar charts are overlappingPython条形图重叠
【发布时间】:2015-10-05 16:24:39
【问题描述】:

我已经使用 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()

生成的条形图如下所示。

我玩弄了 np.arange() 但没有用,我不知道我是否遗漏了什么。另外我想知道我是否可以在图表中提出更多美学色彩。非常感谢任何帮助。

【问题讨论】:

    标签: python numpy matplotlib bar-chart


    【解决方案1】:

    你试过设置width = 0.15吗?我不知道它是否必须在总和以下。

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

    【讨论】:

    • 感谢它的工作,但 x 轴标签将被调整为位于每组条形的中间,并且它们之间的空间也更大。还有一种方法可以防止 xtick_lables 在图表上重叠吗?
    猜你喜欢
    • 2015-12-25
    • 1970-01-01
    • 2019-04-15
    • 2023-01-21
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多