【问题标题】:Pyplot Barchart: Bars not grouping around xticks properlyPyplot Barchart:条形图未正确围绕 xticks 分组
【发布时间】:2019-08-02 19:13:35
【问题描述】:

我试图在条形图中围绕 xticks 分组四个条形。这是一些示例数据(请注意,我在 Python 2.7 中运行它)和我的代码。

import matplotlib.pyplot as plt
import numpy as np

xps_s1 = range(2008, 2019)
xps_s2 = range(2012, 2019)
xps_s3 = range(2013, 2019)
xps_s4 = range(2014, 2019)
yps_s1 = [94.6, 93.9, 93, 94.7, 94.6, 95.4, 95, 93.6, 93, 93.6, 92.2]
yps_s2 = [81.5, 90.2, 91.5, 94, 95, 94.3, 95.3]
yps_s3 = [83.9, 92.7, 93.3, 94.4, 94.4, 94.6]
yps_s4 = [90.6, 95, 94.8, 94, 93.9]

y_means = [94.6, 93.9, 93, 94.7, np.mean([81.5, 94.6]), 
           np.mean([83.9, 90.2, 95.4]), np.mean([92.7, 91.5, 95, 90.6]), 
           np.mean([93.3, 94, 93.6, 95]), np.mean([94.4, 95, 93, 94.8]), 
           np.mean([94.4, 94.3, 93.6, 94]), np.mean([91.4, 94.6, 95.3, 92.2, 93.9])]

fig = plt.subplots()
ax = plt.axes(xlim=(2007,2019), ylim=(75, 100))

w = 0.2
plt.xticks(np.arange(2008, 2019, step = 1))

rects1 = ax.bar([x-w for x in xps_s1], yps_s1, width=w, align="center",
                color='goldenrod', label='Sample1')

rects2 = ax.bar([x-w*2 for x in xps_s2], yps_s2, width=w, align="center",
                color='grey', label='Sample2')

rects3 = ax.bar([x+w for x in xps_s3], yps_s3, width=w, align="center",
                color='silver', label='Sample3')

rects4 = ax.bar([x+w*2 for x in xps_s4], yps_s4, width=w, align="center",
                color='thistle', label='Sample4')

mean_line =ax.plot(xps_s1,y_means, label='Overall', 
                       linestyle='-', color = "indianred")

legend = ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()

当我有三个条时,我设置了 w = 0.3 并且这些条很好地分组在刻度周围(我让 rects1 紧贴在刻度上,另外两个紧靠在它的侧面,其余 0.09 的宽度将年份分开)

现在使用上面的代码,它们似乎真的与任何刻度无关,并且它们没有正确分组。

我做错了什么?

提前非常感谢!

【问题讨论】:

  • 您确定您发布的代码可以运行吗?我得到ValueError: shape mismatch: objects cannot be broadcast to a single shape。确保人们可以运行您的代码
  • 啊,抱歉,rects1 中有错字...已修复
  • 再一次,您的代码无法正常工作,我再次建议您尝试将代码复制粘贴到新的笔记本或编辑器中并运行它。
  • 我已经复制粘贴了,一定是有些变量还在加载。清除变量并修复代码。再次,对不起...

标签: python-2.7 matplotlib


【解决方案1】:

我认为您想使用align='edge' 来简化计算。这是你想要得到的吗?

import matplotlib.pyplot as plt
import numpy as np

xps_s1 = range(2008, 2019)
xps_s2 = range(2012, 2019)
xps_s3 = range(2013, 2019)
xps_s4 = range(2014, 2019)
yps_s1 = [94.6, 93.9, 93, 94.7, 94.6, 95.4, 95, 93.6, 93, 93.6, 92.2]
yps_s2 = [81.5, 90.2, 91.5, 94, 95, 94.3, 95.3]
yps_s3 = [83.9, 92.7, 93.3, 94.4, 94.4, 94.6]
yps_s4 = [90.6, 95, 94.8, 94, 93.9]

y_means = [94.6, 93.9, 93, 94.7, np.mean([81.5, 94.6]), 
           np.mean([83.9, 90.2, 95.4]), np.mean([92.7, 91.5, 95, 90.6]), 
           np.mean([93.3, 94, 93.6, 95]), np.mean([94.4, 95, 93, 94.8]), 
           np.mean([94.4, 94.3, 93.6, 94]), np.mean([91.4, 94.6, 95.3, 92.2, 93.9])]

fig = plt.subplots()
ax = plt.axes(xlim=(2007,2019), ylim=(75, 100))

w = 0.2
plt.xticks(np.arange(2008, 2019, step = 1))

rects1 = ax.bar([x-w for x in xps_s1], yps_s1, width=w, align="edge",
                color='goldenrod', label='Sample1')

rects2 = ax.bar([x-w*2 for x in xps_s2], yps_s2, width=w, align="edge",
                color='grey', label='Sample2')

rects3 = ax.bar([x for x in xps_s3], yps_s3, width=w, align="edge",
                color='silver', label='Sample3')

rects4 = ax.bar([x+w for x in xps_s4], yps_s4, width=w, align="edge",
                color='thistle', label='Sample4')

mean_line =ax.plot(xps_s1,y_means, label='Overall', 
                       linestyle='-', color = "indianred")

legend = ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2014-10-22
    • 2018-12-07
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多