我正在使用檔案中的資料使用 pylab 制作條形圖。當我運行它時,我得到一個很長的錯誤代碼。
這是我的代碼。
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()
這是我收到的錯誤訊息。
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
uj5u.com熱心網友回復:
你有 10 個 x_values 但只有 7 個 y_values。嘗試使用平衡資料運行相同的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340416.html
標籤:Python matplotlib
上一篇:如何使串列具有特定大小(k)
