对象不能广播到单个形状(条形图)
Posted
技术标签:
【中文标题】对象不能广播到单个形状(条形图)【英文标题】:Objects cannot be broadcast to a single shape (Barchart) 【发布时间】:2021-01-23 13:04:40 【问题描述】:我正在尝试使用 matplotlib 绘制条形图,但它向我显示了此错误消息 ValueError:形状不匹配:无法将对象广播到单个形状
这是我的代码:
print(lol)
X = numpy.arange(3)
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, lol[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, lol[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, lol[2], color = 'r', width = 0.25)
plt.show()
这是我的错误信息输出:
这里的数据是需要在条形图中绘制的数据
它以list
的list
的形式存储在lol
中。
[[0, 21, 0, 15, 0, 4, 11, 0, 1, 0, 0, 3, 32, 0, 0, 38, 0, 0, 0, 0, 2, 4, 4, 0, 4, 2, 0, 0, 1, 0, 1, 0, 0, 3, 1, 11, 0, 3, 10, 0, 1, 0, 0, 11, 0, 7, 0, 7, 0, 0, 7, 3, 2, 1, 1, 0, 1, 5, 1, 3, 1, 1, 4, 0, 2, 4, 0, 1, 5, 0, 0, 4, 2, 3, 1, 1, 0, 1, 0, 4, 1, 1, 9, 1, 6, 3, 7, 3, 0, 2, 0, 1, 1, 1, 5, 2, 3, 0, 1], [135, 0, 135, 0, 135, 0, 0, 135, 0, 135, 135, 0, 0, 135, 135, 0, 135, 135, 135, 135, 0, 0, 0, 135, 0, 0, 135, 135, 0, 135, 0, 135, 135, 0, 0, 0, 135, 0, 0, 135, 0, 135, 135, 0, 135, 0, 135, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0, 0, 135, 0, 0, 135, 0, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 135, 0], [143, 0, 143, 0, 143, 0, 0, 143, 0, 143, 143, 0, 0, 143, 143, 0, 143, 143, 143, 143, 0, 0, 0, 143, 0, 0, 143, 143, 0, 143, 0, 143, 143, 0, 0, 0, 143, 0, 0, 143, 0, 143, 143, 0, 143, 0, 143, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 143, 0, 0, 143, 0, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 143, 0]]
错误:
Traceback (most recent call last):
File "E:\Pyton Excel Extractions\dashboard.py", line 68, in <module>
ax.bar(X + 0.00, lol[0], color = 'b', width = 0.25)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\matplotlib\__init__.py", line 1438, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\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\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\lib\stride_tricks.py", line 258, in broadcast_arrays
shape = _broadcast_shape(*args)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38-32\lib\site-packages\numpy\lib\stride_tricks.py", line 189, in _broadcast_shape
b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape
帮帮我!
【问题讨论】:
你想通过arange(3)
实现什么? X
和lol[0]
的长度不一样。
过段时间我会试一试的。
***.com/questions/64279766/…这个@Grayrigel
【参考方案1】:
X
和 lol[0]
、lol[1]
和 lol[2]
具有不同的长度(分别为 3
和 99
)。更改 X
的长度会产生以下输出。这有帮助吗?
import numpy
import matplotlib.pyplot as plt
lol=[[0, 21, 0, 15, 0, 4, 11, 0, 1, 0, 0, 3, 32, 0, 0, 38, 0, 0, 0, 0, 2, 4, 4, 0, 4, 2, 0, 0, 1, 0, 1, 0, 0, 3, 1, 11, 0, 3, 10, 0, 1, 0, 0, 11, 0, 7, 0, 7, 0, 0, 7, 3, 2, 1, 1, 0, 1, 5, 1, 3, 1, 1, 4, 0, 2, 4, 0, 1, 5, 0, 0, 4, 2, 3, 1, 1, 0, 1, 0, 4, 1, 1, 9, 1, 6, 3, 7, 3, 0, 2, 0, 1, 1, 1, 5, 2, 3, 0, 1], [135, 0, 135, 0, 135, 0, 0, 135, 0, 135, 135, 0, 0, 135, 135, 0, 135, 135, 135, 135, 0, 0, 0, 135, 0, 0, 135, 135, 0, 135, 0, 135, 135, 0, 0, 0, 135, 0, 0, 135, 0, 135, 135, 0, 135, 0, 135, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0, 0, 135, 0, 0, 135, 0, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 135, 0], [143, 0, 143, 0, 143, 0, 0, 143, 0, 143, 143, 0, 0, 143, 143, 0, 143, 143, 143, 143, 0, 0, 0, 143, 0, 0, 143, 143, 0, 143, 0, 143, 143, 0, 0, 0, 143, 0, 0, 143, 0, 143, 143, 0, 143, 0, 143, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 143, 0, 0, 143, 0, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 143, 0]]
#changing the length of X such that it matches lol[0],lol[1] etc.
X = numpy.arange(len(lol[0]))
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.bar(X + 0.00, lol[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, lol[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, lol[2], color = 'r', width = 0.25)
plt.show()
输出:
根据您的评论,这样的事情会很好。这里我正在使用pandas
:
import numpy
import pandas as pd
import matplotlib.pyplot as plt
lol=[[0, 21, 0, 15, 0, 4, 11, 0, 1, 0, 0, 3, 32, 0, 0, 38, 0, 0, 0, 0, 2, 4, 4, 0, 4, 2, 0, 0, 1, 0, 1, 0, 0, 3, 1, 11, 0, 3, 10, 0, 1, 0, 0, 11, 0, 7, 0, 7, 0, 0, 7, 3, 2, 1, 1, 0, 1, 5, 1, 3, 1, 1, 4, 0, 2, 4, 0, 1, 5, 0, 0, 4, 2, 3, 1, 1, 0, 1, 0, 4, 1, 1, 9, 1, 6, 3, 7, 3, 0, 2, 0, 1, 1, 1, 5, 2, 3, 0, 1], [135, 0, 135, 0, 135, 0, 0, 135, 0, 135, 135, 0, 0, 135, 135, 0, 135, 135, 135, 135, 0, 0, 0, 135, 0, 0, 135, 135, 0, 135, 0, 135, 135, 0, 0, 0, 135, 0, 0, 135, 0, 135, 135, 0, 135, 0, 135, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0, 0, 135, 0, 0, 135, 0, 0, 135, 135, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135, 0, 135, 0, 0, 0, 0, 0, 0, 135, 0], [143, 0, 143, 0, 143, 0, 0, 143, 0, 143, 143, 0, 0, 143, 143, 0, 143, 143, 143, 143, 0, 0, 0, 143, 0, 0, 143, 143, 0, 143, 0, 143, 143, 0, 0, 0, 143, 0, 0, 143, 0, 143, 143, 0, 143, 0, 143, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 0, 0, 0, 0, 0, 0, 143, 0, 0, 143, 0, 0, 143, 143, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 143, 0, 143, 0, 0, 0, 0, 0, 0, 143, 0]]
df = pd.DataFrame(lol).T
df.columns = ['1st Month','2nd Month', '3rd Month']
df['Product'] = numpy.arange(len(lol[0]))
#if you don't like subplots keep subplots=False
df.plot.bar(x='Product',subplots=True, figsize=(20,10))
plt.show()
输出:
【讨论】:
是的,我看到了它的工作谢谢你能帮我解决这个问题吗?在大声笑中输入的值是列表格式的列表,所以基本上它是第一个列表是第一个月的产品计数,然后是第二个列表第二个月的产品计数等等有 99 种产品,所以我需要为每个产品计数图有 99 种不同的颜色,并且所有 99 种产品必须像在大声笑中那样排列意味着每月所以怎么做 是的,我会的 我已经添加了一个答案。我认为 99 列太多了。我觉得如果你引入子图会很好看。 好吧,我理解你的方法,但最后一件事是我需要每种产品都有不同的颜色,以便可以在一个月内确定哪个产品的销售额最高或最低。 ... 我还想显示具有特定颜色的产品名称,这些名称在列表中定义@Gabyrigel以上是关于对象不能广播到单个形状(条形图)的主要内容,如果未能解决你的问题,请参考以下文章
Microsoft Project甘特图有条竖黄线是啥,怎么去掉