I am trying to create a bar chart like below:
Where each month on the X-axis has three unique values, however when I run my code I get the following error:
Traceback (most recent call last): File "/tmp/sessions/6a06aeb1410cb532/main.py", line 12, in rects1 = ax.bar(ind, yvals, width, color='r') File "/usr/local/lib/python3.6/dist-packages/matplotlib/init.py", line 1867, in inner return func(ax, *args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_axes.py", line 2238, in bar np.atleast_1d(x), height, width, y, linewidth) File "/usr/local/lib/python3.6/dist-packages/numpy/lib/stride_tricks.py", line 252, in broadcast_arrays shape = _broadcast_shape(*args) File "/usr/local/lib/python3.6/dist-packages/numpy/lib/stride_tricks.py", line 187, in _broadcast_shape b = np.broadcast(*args[:32]) ValueError: shape mismatch: objects cannot be broadcast to a single shape
Here is the code in which I am using
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()
Any help in identifying the cause and fix or this error would be greatly appreciated.