【问题标题】:Sorting categorical labels in seaborn chart在 seaborn 图表中对分类标签进行排序
【发布时间】:2017-10-19 11:02:38
【问题描述】:

我确定这应该很容易,但我在 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 代码。请注意,“收入猫”是收入低于 50K 和收入高于 50K

的人的分类分组
plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
                                      hue='income-cat', data=data)

这是目前我的情节的样子(上面的代码中没有包含一些额外的格式,因为不相关):

【问题讨论】:

标签: python pandas visualization seaborn


【解决方案1】:

设置order参数。

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

【讨论】:

    【解决方案2】:

    您可以尝试以下方法,

    sns.countplot(data = data.sort_values(by="hours_per_week_grouping"), 
                  x= "hours_per_week_grouping", hue= "income-cat")
    

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 2021-02-22
      • 2021-12-27
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      相关资源
      最近更新 更多