1

I am trying to create a bar chart like below:

barchart

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.

1
  • N was the main culprit for your code to fail Commented Jan 17, 2021 at 16:35

2 Answers 2

2

The main reason for your code to fail was N value. Since you consider values for 12 months (or datapoints), your N value has to be 12. Checkout the code below

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()

And with a better figsize, you can achieve a clean output like this: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Setting N=12 (the number of datapoints in the three bar-datasets) gives

enter image description here

Is this what you are looking for?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.