Kristada673
Kristada673

Reputation: 3744

How to set a categorical axis in order in Seaborn boxplot?

I have a dataframe that I want to plot the boxplot of and color according to one of the columns in it:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = {'Age': [50, 30, 38, 43, 26, 30, 38, 46, 37, 43, 55, 29, 31, 31, 33, 34, 32, 25, 25, 40, 29, 34, 26, 30, 26, 30, 38, 29, 46, 30, 28, 26, 28, 61, 21, 44, 30, 30, 28, 66, 34, 40, 25, 44, 30, 27, 34, 24, 42, 57, 28, 23, 49, 34, 55, 28, 36, 33, 34, 26],
        'Age Bin': ['(47.0, 51.0]', '(28.0, 33.0]', '(37.0, 42.0]', '(42.0, 47.0]', '(23.0, 28.0]', '(28.0, 33.0]', '(37.0, 42.0]', '(42.0, 47.0]', '(33.0, 37.0]', '(42.0, 47.0]', '(51.0, 56.0]', '(28.0, 33.0]', '(28.0, 33.0]', '(28.0, 33.0]', '(28.0, 33.0]', '(33.0, 37.0]', '(28.0, 33.0]', '(23.0, 28.0]', '(23.0, 28.0]', '(37.0, 42.0]', '(28.0, 33.0]', '(33.0, 37.0]', '(23.0, 28.0]', '(28.0, 33.0]', '(23.0, 28.0]', '(28.0, 33.0]', '(37.0, 42.0]', '(28.0, 33.0]', '(42.0, 47.0]', '(28.0, 33.0]', '(23.0, 28.0]', '(23.0, 28.0]', '(23.0, 28.0]', '(56.0, 61.0]', '(18.0, 23.0]', '(42.0, 47.0]', '(28.0, 33.0]', '(28.0, 33.0]', '(23.0, 28.0]', '(61.0, 66.0]', '(33.0, 37.0]', '(37.0, 42.0]', '(23.0, 28.0]', '(42.0, 47.0]', '(28.0, 33.0]', '(23.0, 28.0]', '(33.0, 37.0]', '(23.0, 28.0]', '(37.0, 42.0]', '(56.0, 61.0]', '(23.0, 28.0]', '(18.0, 23.0]', '(47.0, 51.0]', '(33.0, 37.0]', '(51.0, 56.0]', '(23.0, 28.0]', '(33.0, 37.0]', '(28.0, 33.0]', '(33.0, 37.0]', '(23.0, 28.0]'],
        'Values': [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
        'Class': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]}

data = pd.DataFrame(data, columns=['Age','Age Bin', 'Values', 'Class'])

fig = plt.figure(figsize=(10,5))
sns.boxplot(x='Age Bin', y="Values", hue='Class', data=data)
plt.xticks(rotation=45)

This produces the following plot:

enter image description here

As can be seen, the values in the x-axis are not in increasing order. How do I fix this?

Upvotes: 0

Views: 4224

Answers (1)

Altenh
Altenh

Reputation: 36

You can set the order of your axis based on the documentation found here.

In this case you can set the order by doing a pd.value_counts on your bin data, sorting it by index and then just grabbing the index as so:

order_agebin = pd.value_counts(data['Age Bin']).sort_index().index

Then just modify your sns.boxplot() part as follows:

sns.boxplot(x='Age Bin', y="Values", hue='Class', data=data, order = order_agebin)

Upvotes: 2

Related Questions