seaborn点图可视化

2024-04-28 23:15:59 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我正在绘制一个点图来显示“工人阶级”、“性别”、“职业”和“收入是否超过50K”之间的关系。然而,结果却是一团糟。图例是粘在一起的,图例中的女性和男性都以蓝色显示

#Co-relate categorical features
grid = sns.FacetGrid(train, row='occupation', size=6, aspect=1.6)
grid.map(sns.pointplot, 'workclass', 'exceeds50K', 'sex', palette='deep', markers = ["o", "x"] )
grid.add_legend()

请告知如何适合地块的大小。谢谢


Tags: 关系绘制grid蓝色features图例性别co
1条回答
网友
1楼 · 发布于 2024-04-28 23:15:59

听起来“exceeds50k”是一个分类变量。对于点图,y变量需要是连续的。假设这是您的数据集:

import pandas as pd
import seaborn as sns
df =pd.read_csv("https://raw.githubusercontent.com/katreparitosh/Income-Predictor-Model/master/Database/adult.csv")

我们简化了一些类别以进行绘图,例如:

df['native.country'] = [i if i == 'United-States' else 'others' for i in df['native.country']  ]
df['race'] = [i if i == 'White' else 'others' for i in df['race']  ]

df.head()

    age workclass   fnlwgt  education   education.num   marital.status  occupation  relationship    race    sex capital.gain    capital.loss    hours.per.week  native.country  income
0   90  ?   77053   HS-grad 9   Widowed ?   Not-in-family   White   Female  0   4356    40  United-States   <=50K
1   82  Private 132870  HS-grad 9   Widowed Exec-managerial Not-in-family   White   Female  0   4356    18  United

如果y变量是分类变量,则可能需要使用条形图:

sns.catplot(hue='income',x='sex', palette='deep',data=df,
            col='native.country',
            row='race',kind='count',height=3,aspect=1.6)

enter image description here

如果它是连续的,例如年龄,您可以看到它工作:

grid = sns.FacetGrid(df, row='race', height=3, aspect=1.6)
grid.map(sns.pointplot, 'native.country', 'age', 'sex', palette='deep', markers = ["o", "x"] )
grid.add_legend()

enter image description here

相关问题 更多 >