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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:18:59  来源:igfitidea点击:

ValueError: Invalid RGBA argument: What is causing this error?

pythonmatplotlibrgba

提问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:

以下是输出:

enter image description here

在此处输入图片说明

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 argumentbecause 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(例如thisthisthisthis)的stackoverflow帖子,但似乎没有一个能回答我的问题。

I want to know what could be causing this error. I am using the standard Anacondadistribution for pythonon Ubuntu Mate 16.

我想知道是什么导致了这个错误。我正在使用on的标准Anaconda发行版。pythonUbuntu 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 colorsis wrong, not because an RGBA value is invalid.

错误消息具有误导性。您收到 ValueError 是因为 的形状colors错误,而不是因为 RGBA 值无效。

When coloring each bar a single color, colorshould be an array of length N, where Nis the number of bars. Since there are 8281 bars,

将每个条形着色为单一颜色时,color应该是一个长度为 的数组N,其中N是条形的数量。由于有 8281 个条形,

In [121]: x_data.shape
Out[121]: (8281,)

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

enter image description here

在此处输入图片说明

回答by ImportanceOfBeingErnest

The colorargument 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. dzis 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

那么结果将是

enter image description here

在此处输入图片说明