Home > Software design >  Why am I receiving this error when using pylab to make a bar graph
Why am I receiving this error when using pylab to make a bar graph

Time:10-29

I am using data from a file to make a bar graph using pylab. When I run it I get a very long error code.

This is my code.

bar_width=0.5
x_values=[1,2,3,4,5,6,7,8,9,10]
y_values=[3,2,5,7,4,10,12]
tlabel=["0-10", "11-20", "21-30", "31-40", "41-50", "51-60", "61-70", "71-80", "81-90", "Above 90"]
#pylab.xticks(range(6),rotation=30)
pylab.title("Homicide Occurance by Age")
pylab.bar(x_values,y_values,width=bar_width,tick_label=tlabel, align='center',color='b')
pylab.show()

And here is the error message I am receiving.

Traceback (most recent call last):
  File "C:\Users\mads\OneDrive\Desktop\CSC 130\prog5.py", line 39, in <module>
    pylab.bar(x_values,y_values,width=bar_width,tick_label=tlabel, align='center',color='b')
  File "C:\Users\mads\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2487, in bar
    return gca().bar(
  File "C:\Users\mads\anaconda3\lib\site-packages\matplotlib\__init__.py", line 1447, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\mads\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 2430, in bar
    x, height, width, y, linewidth = np.broadcast_arrays(
  File "<__array_function__ internals>", line 5, in broadcast_arrays
  File "C:\Users\mads\anaconda3\lib\site-packages\numpy\lib\stride_tricks.py", line 538, in broadcast_arrays
    shape = _broadcast_shape(*args)
  File "C:\Users\mads\anaconda3\lib\site-packages\numpy\lib\stride_tricks.py", line 420, in _broadcast_shape
    b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape 

CodePudding user response:

you have 10 x_values but only 7 y_values. Try to run the same code with balanced data.

  • Related