pandas AttributeError: 'str' 对象在 Seaborn 和 Scatterplot 中没有属性 'view'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54030855/
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 06:15:18  来源:igfitidea点击:

AttributeError: 'str' object has no attribute 'view' in Seaborn , Scatterplot

pythonpython-3.xpandasseabornscatter-plot

提问by user8270077

I am getting a bizarre exception in Seaborn.

我在 Seaborn 遇到了一个奇怪的例外。

For a reproducible example:

对于可重现的示例:

toy_data.to_json()
'{"X":{"0":0.12765045,"1":0.0244816152,"2":0.1263715245,"3":0.0246376768,"4":0.1108581319,"5":0.1406719382,"6":0.1358105564,"7":0.1245863432,"8":0.1175445352,"9":0.1188479018,"10":0.1113148159,"11":0.117455495,"12":0.110555662,"13":0.1328567106,"14":0.103064284,"15":0.1119474442,"16":0.119390455,"17":0.1246727756,"18":0.1117827155,"19":0.1169972547},"Y":{"0":0.1241083714,"1":0.1394242378,"2":0.1225010796,"3":0.0077080173,"4":0.1198371354,"5":0.0029026989,"6":0.1259473297,"7":0.0,"8":0.0,"9":0.1214620231,"10":0.1204110739,"11":0.0,"12":0.1194570059,"13":0.0014971676,"14":0.1184584731,"15":0.1212061305,"16":0.1221438778,"17":0.0,"18":0.1209991075,"19":0.0},"Label":{"0":"17","1":"3","2":"17","3":"0","4":"14","5":"21","6":"16","7":"23","8":"20","9":"15","10":"14","11":"20","12":"14","13":"22","14":"13","15":"14","16":"15","17":"23","18":"14","19":"20"},"Probability":{"0":1.0,"1":1.0,"2":1.0,"3":1.0,"4":1.0,"5":1.0,"6":1.0,"7":1.0,"8":1.0,"9":1.0,"10":1.0,"11":1.0,"12":1.0,"13":1.0,"14":1.0,"15":1.0,"16":1.0,"17":1.0,"18":0.9101796407,"19":1.0}}'

toy_data.head()
       X           Y    Label   Probability
0   0.127650    0.124108    17  1.0
1   0.024482    0.139424    3   1.0
2   0.126372    0.122501    17  1.0
3   0.024638    0.007708    0   1.0
4   0.110858    0.119837    14  1.0

sns.scatterplot(x = toy_data.X, y = toy_data.Y, hue = toy_data.Label.values, alpha = 0.5)

AttributeError: 'str' object has no attribute 'view'

Similar exception with this syntax:

此语法的类似异常:

sns.scatterplot(x = 'X', y = 'Y', data = toy_data, hue = 'Label', alpha = 0.5)

回答by Jorge

As presented, your column Label is an object. It need to be a number (int or float).

如图所示,您的列 Label 是一个对象。它必须是一个数字(整数或浮点数)。

toy_data.info()

<class 'pandas.core.frame.DataFrame'>
Index: 20 entries, 0 to 9
Data columns (total 4 columns):
X              20 non-null float64
Y              20 non-null float64
Label          20 non-null object
Probability    20 non-null float64
dtypes: float64(3), object(1)
memory usage: 800.0+ bytes

Do this:

做这个:

toy_data.loc[:,'Label'] = toy_data.Label.astype(np.float)

to change it to float.

将其更改为浮动。

Then your command:

然后你的命令:

sns.scatterplot(x = toy_data.X, y = toy_data.Y, hue = toy_data.label.values, alpha = 0.5)

should work.

应该管用。

enter image description here

在此处输入图片说明

I am using this command to generate the data frame

我正在使用此命令生成数据框

dict = {"X":{"0":0.12765045,"1":0.0244816152,"2":0.1263715245,"3":0.0246376768,"4":0.1108581319,"5":0.1406719382,"6":0.1358105564,"7":0.1245863432,"8":0.1175445352,"9":0.1188479018,"10":0.1113148159,"11":0.117455495,"12":0.110555662,"13":0.1328567106,"14":0.103064284,"15":0.1119474442,"16":0.119390455,"17":0.1246727756,"18":0.1117827155,"19":0.1169972547},"Y":{"0":0.1241083714,"1":0.1394242378,"2":0.1225010796,"3":0.0077080173,"4":0.1198371354,"5":0.0029026989,"6":0.1259473297,"7":0.0,"8":0.0,"9":0.1214620231,"10":0.1204110739,"11":0.0,"12":0.1194570059,"13":0.0014971676,"14":0.1184584731,"15":0.1212061305,"16":0.1221438778,"17":0.0,"18":0.1209991075,"19":0.0},"Label":{"0":"17","1":"3","2":"17","3":"0","4":"14","5":"21","6":"16","7":"23","8":"20","9":"15","10":"14","11":"20","12":"14","13":"22","14":"13","15":"14","16":"15","17":"23","18":"14","19":"20"},"Probability":{"0":1.0,"1":1.0,"2":1.0,"3":1.0,"4":1.0,"5":1.0,"6":1.0,"7":1.0,"8":1.0,"9":1.0,"10":1.0,"11":1.0,"12":1.0,"13":1.0,"14":1.0,"15":1.0,"16":1.0,"17":1.0,"18":0.9101796407,"19":1.0}}
toy_data = pd.DataFrame(dict)

You will need numpy import numpy as npto convert the values to float

您将需要 numpyimport numpy as np将值转换为浮点数

回答by Prageeth Jayathissa

This is a peculiar issue with seaborn where the color paletteneeds to equal the number of labels

这是 seaborn 的一个特殊问题,其中颜色palette需要等于标签的数量

sns.scatterplot(x = toy_data.X, y = toy_data.Y, hue = toy_data.Label, alpha = 0.5,
                palette=sns.color_palette("Set1", toy_data.Label.nunique()) )

If not, seaborn will apply numerical functions to map the number of huecategories to the default paletteof fourcolours. This is why you get an error when the values are a string

如果没有,seaborn 将应用数值函数将hue类别数量映射到默认palette四种颜色。这就是为什么当值是字符串时会出现错误的原因