【问题标题】:Invalid rgba arg "#" in matplotlibmatplotlib 中的 rgba arg“#”无效
【发布时间】:2015-11-06 00:48:49
【问题描述】:

尝试在 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:无效的 rgb 参数“#” 无法将字符串转换为浮点数:#

它似乎将这些 rgb 参数视为浮点数。

但是在 matplotlib 文档中,颜色是用这种风格写的 http://matplotlib.org/api/colors_api.html

我错过了什么?

【问题讨论】:

标签: python matplotlib scatter-plot


【解决方案1】:

这只是一个简单的错字:您在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()

【讨论】:

    猜你喜欢
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-04-01
    • 2018-08-05
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多