pandas ValueError :“color kwarg 每个数据集必须有一种颜色” matplotlib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50559775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:36:39  来源:igfitidea点击:

ValueError : "color kwarg must have one color per dataset" matplotlib

pythonpandasmatplotlib

提问by Willze Fortner

So, I got an error when I compile my python code (matplot) which is :

所以,当我编译我的 python 代码(matplot)时出现错误:

ValueError : color kwarg must have one color per dataset

ValueError : color kwarg 每个数据集必须有一种颜色

The code which generates this error :

产生此错误的代码:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import pickle as pkl
from __future__ import division

sns.set(style="darkgrid")
sns.distplot(featureSet[featureSet['label']=='0']['len of url'],color='green',label='Benign URLs')
sns.distplot(featureSet[featureSet['label']=='1']['len of url'],color='red',label='Phishing URLs')
sns.plt.title('Url Length Distribution')
plt.legend(loc='upper right')
plt.xlabel('Length of URL')

sns.plt.show()

采纳答案by ImportanceOfBeingErnest

Don't use sns.plt. Instead import matplotlib.pyplot as pltand use it directly.

不要使用sns.plt. 而是import matplotlib.pyplot as plt直接使用它。

Appart the code should run fine. I created the following minimal runnable example

Appart 代码应该可以正常运行。我创建了以下最小的可运行示例

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

df = pd.DataFrame({"label" : np.random.randint(2, size=100).astype(str),
                    "data" : np.random.rayleigh(size=100)})

sns.set(style="darkgrid")
sns.distplot(df[df['label']=='0']['data'],color='green',label='Benign URLs')
sns.distplot(df[df['label']=='1']['data'],color='red',label='Phishing URLs')
plt.title('Url Length Distribution')
plt.legend(loc='upper right')
plt.xlabel('Length of URL')

plt.show()

which produces the following output

产生以下输出

enter image description here

在此处输入图片说明

If it does not work for you, consider upgrading your matplotlib and seaborn version. The above is produced with matplotlib 2.2.2 and seaborn 8.1.

如果它不适合您,请考虑升级您的 matplotlib 和 seaborn 版本。以上是使用 matplotlib 2.2.2 和 seaborn 8.1 生成的。

回答by Harvey

Just delet color atribute.

只需删除颜色属性。

Delete color='green'and color = 'red'and everything will work ok.

删除color='green'color = 'red'一切都会正常工作。