使用 MatPlotLib 将多个条形图绘制成图表
Posted
技术标签:
【中文标题】使用 MatPlotLib 将多个条形图绘制成图表【英文标题】:Plotting multiple bars into a chart using MatPlotLib 【发布时间】:2021-04-22 01:37:53 【问题描述】:我正在尝试创建如下条形图:
X 轴上的每个月都有三个唯一值,但是当我运行我的代码时,我收到以下错误:
Traceback(最近一次调用最后一次): 文件“/tmp/sessions/6a06aeb1410cb532/main.py”,第 12 行,在 rects1 = ax.bar(ind,yvals,宽度,颜色='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 行,在 bar np.atleast_1d(x), 高度, 宽度, y, 线宽) 文件“/usr/local/lib/python3.6/dist-packages/numpy/lib/stride_tricks.py”,第 252 行,在 broadcast_arrays 形状 = _broadcast_shape(*args) _broadcast_shape 中的文件“/usr/local/lib/python3.6/dist-packages/numpy/lib/stride_tricks.py”,第 187 行 b = np.broadcast(*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 是您的代码失败的罪魁祸首 【参考方案1】:您的代码失败的主要原因是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
,您可以实现这样的干净输出:
【讨论】:
【参考方案2】:设置N=12
(三个条形数据集中的数据点数)给出
这是你要找的吗?
【讨论】:
以上是关于使用 MatPlotLib 将多个条形图绘制成图表的主要内容,如果未能解决你的问题,请参考以下文章
使用 ax.bar() 使用 matplotlib 绘制多个条形图