ValueError:无效的RGBA参数:'rgbkymc'

时间:2018-06-11 16:41:30

标签: python-3.x matplotlib

train_class = train_df['Class'].value_counts().sortlevel()
my_colors = 'rgbkymc'  #red, green, blue, black, etc.
train_class.plot(kind='bar', color=my_colors)
plt.grid()
plt.show()

获取值错误:RGBA参数无效:'rgbkymc'

不知道为什么我检查一切都很好。 任何人都可以帮我识别错误吗?

KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba(c, alpha)
131     try:
--> 132         rgba = _colors_full_map.cache[c, alpha]
133     except (KeyError, TypeError):  # Not in cache, or unhashable.

KeyError: ('rgbkymc', None)

3 个答案:

答案 0 :(得分:7)

该问题需要稍作修改,因为它首先会引发以下错误:

AttributeError: 'Series' object has no attribute 'sortlevel'

这是因为sortlevel自0.20.0版以来已被弃用。您应该改为使用sort_index

此外,color命令的plot参数中代表颜色的字母需要在列表中提供,而不是在字符串中提供。您可以在Specifying Colors上了解有关此内容的更多信息。

因此,您可以使用以下代码:

train_class = train_df['Class'].value_counts().sort_index()
my_colors = ['r', 'g', 'b', 'k', 'y', 'm', 'c']  #red, green, blue, black, 'yellow', 'magenta' & 'cyan'
train_class.plot(kind = 'bar', color = my_colors)
plt.grid()
plt.show()

谢谢!

答案 1 :(得分:3)

my_colors = ['r', 'g', 'b', 'k', 'y', 'm', 'c']  #red, green, blue, black, 'yellow', 'magenta' & 'cyan'
train_class_distribution.plot(kind='bar',color=my_colors)

答案 2 :(得分:1)

color参数只需valid color value ,因此'r''k'序列这些颜色值(documentation for bar()称之为数组)。名称的列表将起作用:

my_colors = ['r', 'g', 'b', 'k', 'y', 'm', 'c']  # red, green, blue, black, etc.

文档说明序列的长度应与绘制的条形数相等:

  

可选参数 color edgecolor linewidth xerr yerr 可以是标量或长度等于条数的序列。

您可能还希望将color maps视为一种方便快捷的条形颜色;你无法直接传递这些内容,但可以 create your series of colors from a colormap once imported

import matplotlib.pyplot as plt

paired_colors = plt.cm.Paired(range(len(train_class)))

train_class.plot(kind='bar', color=paired_colors)

对于条形图,我选择qualitative colormap;每个名称都是plt.cm模块的属性。