matplotlib 中的无效 rgba arg“#”
Invalid rgba arg "#" in matplotlib
我在 matplotlib 中尝试创建散点图时不知道如何使用颜色。
我正在尝试绘制具有不同颜色点的多个散点图以显示聚类。
colors=['#12efff','#eee111','#eee00f','#e00fff','#123456','#abc222','#000000','#123fff','#1eff1f','#2edf4f','#2eaf9f','#22222f'
'#eeeff1','#eee112','#00ef00','#aa0000','#0000aa','#000999','#32efff','#23ef68','#2e3f56','#7eef1f','#eeef11']
C=1
fig = plt.figure()
ax = fig.gca(projection='3d')
for fgroups in groups:
X=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
y=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
Z=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
C=(C+1) % len(colors)
ax.scatter(X,Y,Z, s=20, c=colors[C], depthshade=True)
plt.show()
我得到的错误如下:
ValueError: to_rgba: Invalid rgba arg "#"
to_rgb: Invalid rgb arg "#"
could not convert string to float: #
它似乎将这些 rgb 参数视为浮点数。
但是在 matplotlib 文档中,颜色是以这种风格编写的 http://matplotlib.org/api/colors_api.html
我错过了什么?
这只是一个简单的拼写错误:您在 colors
的第一行末尾(在 '#22222f'
和 '#eeeff1'
之间)缺少一个逗号,这意味着两个条目被连接起来变成 matplotlib
无法解释为颜色的字符串。
for i in colors:
print i
#12efff
#eee111
#eee00f
#e00fff
#123456
#abc222
#000000
#123fff
#1eff1f
#2edf4f
#2eaf9f
#22222f#eeeff1
#eee112
#00ef00
#aa0000
#0000aa
#000999
#32efff
#23ef68
#2e3f56
#7eef1f
#eeef11
如果你在那里加上逗号,一切似乎都很好。
为了让你的 "MCVE" 工作,我必须添加模块导入,并假设 groups
的长度与 colors
:
相同
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
colors=['#12efff','#eee111','#eee00f','#e00fff','#123456','#abc222','#000000','#123fff','#1eff1f','#2edf4f','#2eaf9f','#22222f',
'#eeeff1','#eee112','#00ef00','#aa0000','#0000aa','#000999','#32efff','#23ef68','#2e3f56','#7eef1f','#eeef11']
C=1
fig = plt.figure()
ax = fig.gca(projection='3d')
groups = range(len(colors))
for fgroups in groups:
X=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
Y=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
Z=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
C=(C+1) % len(colors)
ax.scatter(X,Y,Z, s=20, c=colors[C], depthshade=True)
plt.show()
我在 matplotlib 中尝试创建散点图时不知道如何使用颜色。
我正在尝试绘制具有不同颜色点的多个散点图以显示聚类。
colors=['#12efff','#eee111','#eee00f','#e00fff','#123456','#abc222','#000000','#123fff','#1eff1f','#2edf4f','#2eaf9f','#22222f'
'#eeeff1','#eee112','#00ef00','#aa0000','#0000aa','#000999','#32efff','#23ef68','#2e3f56','#7eef1f','#eeef11']
C=1
fig = plt.figure()
ax = fig.gca(projection='3d')
for fgroups in groups:
X=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
y=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
Z=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
C=(C+1) % len(colors)
ax.scatter(X,Y,Z, s=20, c=colors[C], depthshade=True)
plt.show()
我得到的错误如下:
ValueError: to_rgba: Invalid rgba arg "#" to_rgb: Invalid rgb arg "#" could not convert string to float: #
它似乎将这些 rgb 参数视为浮点数。
但是在 matplotlib 文档中,颜色是以这种风格编写的 http://matplotlib.org/api/colors_api.html
我错过了什么?
这只是一个简单的拼写错误:您在 colors
的第一行末尾(在 '#22222f'
和 '#eeeff1'
之间)缺少一个逗号,这意味着两个条目被连接起来变成 matplotlib
无法解释为颜色的字符串。
for i in colors:
print i
#12efff
#eee111
#eee00f
#e00fff
#123456
#abc222
#000000
#123fff
#1eff1f
#2edf4f
#2eaf9f
#22222f#eeeff1
#eee112
#00ef00
#aa0000
#0000aa
#000999
#32efff
#23ef68
#2e3f56
#7eef1f
#eeef11
如果你在那里加上逗号,一切似乎都很好。
为了让你的 "MCVE" 工作,我必须添加模块导入,并假设 groups
的长度与 colors
:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
colors=['#12efff','#eee111','#eee00f','#e00fff','#123456','#abc222','#000000','#123fff','#1eff1f','#2edf4f','#2eaf9f','#22222f',
'#eeeff1','#eee112','#00ef00','#aa0000','#0000aa','#000999','#32efff','#23ef68','#2e3f56','#7eef1f','#eeef11']
C=1
fig = plt.figure()
ax = fig.gca(projection='3d')
groups = range(len(colors))
for fgroups in groups:
X=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
Y=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
Z=[np.random.rand(50),np.random.rand(50),np.random.rand(50)]
C=(C+1) % len(colors)
ax.scatter(X,Y,Z, s=20, c=colors[C], depthshade=True)
plt.show()