在seaborn图表中对分类标签进行排序 [英] Sorting categorical labels in seaborn chart
问题描述
我确定这应该很容易,但是我在 seaborn 文档中找不到我是如何具体选择要显示的 x 标签分类变量的顺序的?
I'm sure this should be easy, but I can't find in the seaborn documentation how I specifically choose the order of my x label categorical variables which are to be displayed?
我每周工作的时间如下,但我想在我的 x 轴上从 0-10、11-20、21-30、31-40 和超过 40 小时订购这些.我该怎么做?
I have hours worked per week as follows below, but I want to order these on my x axis from 0-10, 11-20, 21-30, 31-40 and More than 40 hours. How can I do this?
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 代码.请注意,收入猫"是收入低于 50K 的人和收入高于 50K
This is my seaborn code to plot the chart. Note 'income-cat' is a categorical grouping of people earning under 50K and those earning above 50K
plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
hue='income-cat', data=data)
这是目前我的情节的样子(上面的代码中没有包含一些额外的格式,因为不相关):
This is currently how my plot looks (with some additional formatting not included in the code above because not relevant):
推荐答案
设置order
参数.
plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
hue='income-cat', data=data, order=['place the desired order here'])
这篇关于在seaborn图表中对分类标签进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!