在 seaborn 图表中对分类标签进行排序
Sorting categorical labels in seaborn chart
我确定这应该很容易,但我在 seaborn 文档中找不到如何具体选择要显示的 x 标签分类变量的顺序?
我每周的工作时间如下,但我想在我的 x 轴上从 0-10、11-20、21-30、31-40 和超过 40 小时排序。我该怎么做?
def hours_per_week (x):
if x < 11: return '0-10 hours'
elif x < 21: return '11-20 hours'
elif x < 31: return '21-30 hours'
elif x < 41: return '31-40 hours'
else: return 'More than 40 hours'
这是我绘制图表的 seaborn 代码。注意 'income-cat' 是收入 低于 50K 的人群和收入 高于 50K
的人群
plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
hue='income-cat', data=data)
这是我的情节目前的样子(上面的代码中没有包含一些额外的格式,因为不相关):
设置order
参数。
plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
hue='income-cat', data=data, order=['place the desired order here'])
您可以尝试以下方法,
sns.countplot(data = data.sort_values(by="hours_per_week_grouping"),
x= "hours_per_week_grouping", hue= "income-cat")
我确定这应该很容易,但我在 seaborn 文档中找不到如何具体选择要显示的 x 标签分类变量的顺序?
我每周的工作时间如下,但我想在我的 x 轴上从 0-10、11-20、21-30、31-40 和超过 40 小时排序。我该怎么做?
def hours_per_week (x):
if x < 11: return '0-10 hours'
elif x < 21: return '11-20 hours'
elif x < 31: return '21-30 hours'
elif x < 41: return '31-40 hours'
else: return 'More than 40 hours'
这是我绘制图表的 seaborn 代码。注意 'income-cat' 是收入 低于 50K 的人群和收入 高于 50K
的人群plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
hue='income-cat', data=data)
这是我的情节目前的样子(上面的代码中没有包含一些额外的格式,因为不相关):
设置order
参数。
plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
hue='income-cat', data=data, order=['place the desired order here'])
您可以尝试以下方法,
sns.countplot(data = data.sort_values(by="hours_per_week_grouping"),
x= "hours_per_week_grouping", hue= "income-cat")