在matplotlib中,"#"(无效的rgba参数)是什么意思?

5
我不知道在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:无效的rgba参数“#” to_rgb:无效的rgb参数“#” 无法将字符串转换为浮点数:#
看起来它把这些rgb参数当作浮点数处理。
然而,在matplotlib文档中,颜色以这种样式编写http://matplotlib.org/api/colors_api.html 我错过了什么?

1
一个 [mcve] 对我们非常有帮助。 - cel
1个回答

2

这只是一个简单的拼写错误:你在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()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接