ValueError:无效的RGBA参数:'rgbkymc' [英] ValueError: Invalid RGBA argument: 'rgbkymc'

查看:35
本文介绍了ValueError:无效的RGBA参数:'rgbkymc'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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()

我得到:

Value Error : Invalid RGBA argument : 'rgbkymc'

我不知道为什么我会收到这个错误,因为我已经检查了所有东西并且看起来很好.

I can't get the reason why I'm getting this error as I have checked everything and it seems fine.

谁能帮我找出错误?

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)

推荐答案

Dataframe.plot() 实际上并不采用 color 参数.您必须驱动 matplotlib.pyplot.bar() 如果要使用简单的颜色序列,则直接调用(但请注意,下面列出了更好的选择).

Dataframe.plot() doesn't actually take a color argument. You'd have to drive a matplotlib.pyplot.bar() call directly if you wanted to use a simple sequence of colours (but note that there are better options, listed below).

如果您确实决定直接使用 matplotlib.pyplot.bar(),请考虑到它的 color 参数,然后只使用单个有效颜色值,所以 'r''k',或这些颜色值的序列(

If you do decide to use matplotlib.pyplot.bar() directly, then take into account that it's color argument then only takes either a single valid color value, so 'r' or 'k', or a sequence of such color values (the documentation for bar() calls it array like). A list of names would work:

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

plt.bar(len(train_class), train_class, color=my_colors)

文档指出,序列的长度应等于绘制的条形数量:

The documentation states that the sequence should be equal in length to the number of bars plotted:

可选参数 coloredgecolorlinewidthxerryerr可以是标量,也可以是长度等于条数的序列.

The optional arguments color, edgecolor, linewidth, xerr, and yerr can be either scalars or sequences of length equal to the number of bars.

但是,在这里将颜色图传递给 Dataframe.plot()只是比较容易.颜色图是一种方便快捷的方法,可实现不同的条形颜色.您可以传入一个作为 colormap 关键字参数,它可以是一个命名映射(作为字符串):

However, it is just easier to pass in a color map to Dataframe.plot() here. Color maps as a handy and fast path towards distinct bar colors. You can pass one in as the colormap keyword argument, this can be a named map (as a string):

train_class.plot(kind='bar', colormap='Paired')

matplotlib.cm 模块中的实际matplotlib颜色图对象:

or an actual matplotlib colormap object from the matplotlib.cm module:

from matplotlib import cm

train_class.plot(kind='bar', colormap=cm.Paired)

如果您想坚持使用 matplotlib.pyplot.bar(),但要使用颜色表,则可以从颜色表中创建一系列颜色.熊猫为此使用 np.linspace(),因此在这里我们也这样做:

If you wanted to stick with matplotlib.pyplot.bar(), but use a colormap, then create your series of colors from a colormap. Pandas uses np.linspace() for this so here we do too:

import numpy as np

paired_colors = cm.Paired(np.linspace(0, 1, num=len(train_class))
plt.bar(len(train_class), train_class, color=paired_colors)

对于条形图,我选择一个定性颜色图;每个名称都是 cm 颜色图模块的属性.在上面, cm.Paired 是一个这样的颜色图.使用介于 0.0 和 1.0 之间的一系列浮点数调用颜色图可以为您提供在该范围的每个百分比"处选取的背景颜色.您也可以传入整数序列来索引各个颜色.

For bar plots, I'd pick a qualitative colormap; each name is an attribute of the cm colormap module. In the above, cm.Paired is a one such color map. Calling the color map with a sequence of floats between 0.0 and 1.0 gives you back colours picked at each 'percentage' of the range. You could also pass in a sequence of integers to index individual colours instead.

回到 Pandas,您也可以使用

Circling back to Pandas, you can create a colormap from a hand-picked sequence of colours too, with a matplotlib.colors.ListedColormap instance:

from matplotlib.colors import ListedColormap

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

然后将其传递给您的数据框 .plot() 调用:

and then pass that to your dataframe .plot() call:

train_class.plot(kind='bar', colormap=my_colormap)

这篇关于ValueError:无效的RGBA参数:'rgbkymc'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆