Python 使用MatPlotLib将多个条形图打印到图表中

Python 使用MatPlotLib将多个条形图打印到图表中,python,matplotlib,Python,Matplotlib,我正在尝试创建一个条形图,如下所示: 其中,X轴上的每个月都有三个唯一的值,但是当我运行代码时,会出现以下错误: 回溯(最近一次呼叫最后一次): 文件“/tmp/sessions/6a06aeb1410cb532/main.py”,第12行,在 rects1=ax.bar(ind,yvals,width,color='r') 文件“/usr/local/lib/python3.6/dist packages/matplotlib/init.py”,第1867行,内部 返回函数(ax,*args

我正在尝试创建一个条形图,如下所示:

其中,X轴上的每个月都有三个唯一的值,但是当我运行代码时,会出现以下错误:

回溯(最近一次呼叫最后一次): 文件“/tmp/sessions/6a06aeb1410cb532/main.py”,第12行,在 rects1=ax.bar(ind,yvals,width,color='r') 文件“/usr/local/lib/python3.6/dist packages/matplotlib/init.py”,第1867行,内部 返回函数(ax,*args,**kwargs) 文件“/usr/local/lib/python3.6/dist packages/matplotlib/axes/_axes.py”,第2238行,在条形图中 np.至少1d(x)、高度、宽度、y、线宽) 文件“/usr/local/lib/python3.6/dist-packages/numpy/lib/stride\u-tricks.py”,第252行,在广播数组中 形状=_广播_形状(*args) 文件“/usr/local/lib/python3.6/dist-packages/numpy/lib/stride\u-tricks.py”,第187行,呈广播形状 b=np.广播(*args[:32]) ValueError:形状不匹配:无法将对象广播到单个形状

下面是我正在使用的代码

import numpy as np
import matplotlib.pyplot as plt

N = 3
ind = np.arange(N)  # the x locations for the groups
width = 0.27       # the width of the bars

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

yvals = [1128, 902, 788, 431, 536, 925, 1001, 853, 1115, 1059, 685, 876]
rects1 = ax.bar(ind, yvals, width, color='r')

zvals = [2500,2085, 1931, 1147, 1218, 2056, 1943, 1805, 2218, 2427, 1467, 1966]
rects2 = ax.bar(ind+width, zvals, width, color='g')

kvals = [1042,847,764, 464, 483, 757, 842, 724, 958, 902, 668, 847]
rects3 = ax.bar(ind+width*2, kvals, width, color='b')

ax.set_ylabel('Average traffic count')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') )
 ax.legend( (rects1[0], rects2[0], rects3[0]), ('London Road', 'Norwich Road', 'Foxhall Road') )

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

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

plt.show()
如果您能帮助确定原因并修复此错误,我们将不胜感激。

设置
N=12
(三个条形数据集中的数据点数量)给出


这就是您要寻找的吗?

代码失败的主要原因是
N
值。既然你考虑了12个月的值(或者数据池),那么你的n值必须是12。检查下面的代码

import numpy as np
import matplotlib.pyplot as plt

N = 12
ind = np.arange(N)  # the x locations for the groups
W = 0.27       # the width of the bars

fig = plt.figure(figsize=(30,10))
ax = fig.add_subplot(111)

yvals = [1128, 902, 788, 431, 536, 925, 1001, 853, 1115, 1059, 685, 876]
rects1 = ax.bar(ind, yvals, width=W, color='r')

zvals = [2500,2085, 1931, 1147, 1218, 2056, 1943, 1805, 2218, 2427, 1467, 1966]
rects2 = ax.bar(ind+W, zvals, width=W, color='g')

kvals = [1042,847,764, 464, 483, 757, 842, 724, 958, 902, 668, 847]
rects3 = ax.bar(ind+W*2, kvals, width=W, color='b')

ax.set_ylabel('Average traffic count')
ax.set_xticks(ind+W)
ax.set_xticklabels( ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December') )
ax.legend( (rects1[0], rects2[0], rects3[0]), ('London Road', 'Norwich Road', 'Foxhall Road') )

def autolabel(rects):
    for rect in rects:
        h = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.02*h, '%d'%int(h),ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

plt.show()
使用更好的
figsize
,您可以获得如下清晰的输出:

完美-谢谢:)N是导致代码失败的主要原因