pandas 在seaborn图表中对分类标签进行排序

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

Sorting categorical labels in seaborn chart

pythonpandasvisualizationseaborn

提问by Alvis

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?

我确定这应该很容易,但是我在 seaborn 文档中找不到我如何具体选择要显示的 x 标签分类变量的顺序?

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?

我每周工作的时间如下,但我想在我的 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'

This is my seaborn code to plot the chart. Note 'income-cat' is a categorical grouping of people earning under 50Kand those earning above 50K

这是我绘制图表的 seaborn 代码。注意“收入猫”是收入低于 50K 的人和收入高于 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): enter image description here

这就是我的情节目前的样子(上面的代码中没有包含一些额外的格式,因为不相关): 在此处输入图片说明

回答by Amit Wolfenfeld

try this:

尝试这个:

plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
                        hue='income-cat', data=data,order=['place the desired order here'])