Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting categorical labels in seaborn chart

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?

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'

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): enter image description here

like image 221
Ali Avatar asked May 19 '17 10:05

Ali


People also ask

Can SNS Countplot () be used to see the frequency distribution of a categorical variable?

countplot. Show the counts of observations in each categorical bin using bars. A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable.

What is Countplot in Seaborn?

countplot() method is used to Show the counts of observations in each categorical bin using bars. Syntax : seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)


2 Answers

Set the order parameter.

plot_income_cat_hours = sns.countplot(x='hours_per_week_grouping',
                        hue='income-cat', data=data, order=['place the desired order here'])
like image 118
Amit Wolfenfeld Avatar answered Oct 20 '22 22:10

Amit Wolfenfeld


You may try the following,

sns.countplot(data = data.sort_values(by="hours_per_week_grouping"), 
              x= "hours_per_week_grouping", hue= "income-cat")
like image 32
BEATHUB Avatar answered Oct 20 '22 23:10

BEATHUB