2015-11-06 50 views
2

我无法弄清楚如何在尝试在matplotlib中创建散点图时使用颜色。matplotlib中的rgba arg“#”无效

我试图绘制不同颜色点的多个散点图来显示群集。

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

我缺少什么?

+1

A [MCVE]会对我们非常有帮助。 – cel

+0

@cel mcve附加 – cjds

回答

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