Python ValueError: Invalid RGBA argument: 是什么导致了这个错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53531429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: Invalid RGBA argument: What is causing this error?
提问by Inspired_Blue
I am trying to create a 3D colored bar chart using ideas from: this stackoverflow post.
我正在尝试使用以下想法创建一个 3D 彩色条形图:this stackoverflow post。
First I create a 3D bar chart with the following code:
首先,我使用以下代码创建一个 3D 条形图:
import numpy as np
import matplotlib.colors as colors
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
samples = np.random.randint(91,size=(5000,2))
F = np.zeros([91,91])
for s in samples:
F[s[0],s[1]] += 1
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_data, y_data = np.meshgrid( np.arange(F.shape[1]),
np.arange(F.shape[0]) )
x_data = x_data.flatten()
y_data = y_data.flatten()
z_data = F.flatten()
ax.bar3d(x_data,y_data,np.zeros(len(z_data)),1,1,z_data )
plt.show()
The following is the output:
以下是输出:
Now I try to color the bars using code verbatimfrom: this stackoverflow post. Here is the code:
现在我尝试使用以下代码逐字着色条形图:this stackoverflow post。这是代码:
import numpy as np
import matplotlib.colors as colors
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
samples = np.random.randint(91,size=(5000,2))
F = np.zeros([91,91])
for s in samples:
F[s[0],s[1]] += 1
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_data, y_data = np.meshgrid( np.arange(F.shape[1]),
np.arange(F.shape[0]) )
x_data = x_data.flatten()
y_data = y_data.flatten()
z_data = F.flatten()
dz = F
offset = dz + np.abs(dz.min())
fracs = offset.astype(float)/offset.max()
norm = colors.Normalize(fracs.min(), fracs.max())
colors = cm.jet(norm(fracs))
# colors = np.random.rand(91,91,4)
ax.bar3d(x_data,y_data,np.zeros(len(z_data)),1,1,z_data,color=colors )
plt.show()
However I get: ValueError: Invalid RGBA argument:
但是我得到: ValueError: Invalid RGBA argument:
Now I am unable to debug the Invalid RGBA argument
because I don't understand what is causing the error. I even tried to use random colors instead with colors = np.random.rand(91,91,4)
and still the error persists.
现在我无法调试,Invalid RGBA argument
因为我不明白是什么导致了错误。我什至尝试使用随机颜色代替,colors = np.random.rand(91,91,4)
但错误仍然存在。
I have checked stackoverflow posts regarding Invalid RGBA argument
(for example this,this,thisand this) and none of that seems to answer my problem.
我已经检查了有关Invalid RGBA argument
(例如this、this、this和this)的stackoverflow帖子,但似乎没有一个能回答我的问题。
I want to know what could be causing this error. I am using the standard Anaconda
distribution for python
on Ubuntu Mate 16
.
我想知道是什么导致了这个错误。我正在使用on的标准Anaconda
发行版。python
Ubuntu Mate 16
Could it be that due to recent updates in python, the solution as in the original stackoverflow postbecomes obsolete?
是不是由于最近的python更新,原始stackoverflow帖子中的解决方案已经过时了?
采纳答案by unutbu
The error message is misleading. You're getting a ValueError because the shape of colors
is wrong, not because an RGBA value is invalid.
错误消息具有误导性。您收到 ValueError 是因为 的形状colors
错误,而不是因为 RGBA 值无效。
When coloring each bar a single color, color
should be an array of length N
, where N
is the number of bars. Since there are 8281 bars,
将每个条形着色为单一颜色时,color
应该是一个长度为 的数组N
,其中N
是条形的数量。由于有 8281 个条形,
In [121]: x_data.shape
Out[121]: (8281,)
colors
should have shape (8281, 4). But instead, the posted code generates an array of shape (91, 91, 4):
colors
应该有形状 (8281, 4)。但是,发布的代码生成了一个形状为 (91, 91, 4) 的数组:
In [123]: colors.shape
Out[123]: (91, 91, 4)
So to fix the problem, use color=colors.reshape(-1,4)
.
因此,要解决此问题,请使用color=colors.reshape(-1,4)
.
import numpy as np
import matplotlib.colors as colors
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
samples = np.random.randint(91,size=(5000,2))
F = np.zeros([91,91])
for s in samples:
F[s[0],s[1]] += 1
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_data, y_data = np.meshgrid( np.arange(F.shape[1]),
np.arange(F.shape[0]) )
x_data = x_data.flatten()
y_data = y_data.flatten()
z_data = F.flatten()
dz = F
offset = dz + np.abs(dz.min())
fracs = offset.astype(float)/offset.max()
norm = colors.Normalize(fracs.min(), fracs.max())
colors = cm.jet(norm(fracs))
ax.bar3d(x_data,y_data,np.zeros(len(z_data)),1,1,z_data,color=colors.reshape(-1,4) )
plt.show()
回答by ImportanceOfBeingErnest
The color
argument expects a 1D array, similar to all other arguments of bar3d
.
该color
参数需要一个一维数组,类似于 的所有其他参数bar3d
。
Hence, you need to replace the line offset = dz + np.abs(dz.min())
因此,您需要更换线路 offset = dz + np.abs(dz.min())
by
经过
offset = z_data + np.abs(z_data.min())
for your case. dz
is not useful here (maybe it was in the linked example).
Note that color=np.random.rand(len(z_data),4)
would equally work.
对于你的情况。dz
在这里没有用(也许在链接的例子中)。
请注意,color=np.random.rand(len(z_data),4)
这同样有效。
Then the result will be
那么结果将是