Skip to content
Related Articles

Related Articles

Improve Article

How To Manually Order Boxplot in Seaborn?

  • Last Updated : 26 Nov, 2020

Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated into the data structures from pandas.
Seaborn aims to make visualization of the central part of exploring and understanding data. It provides dataset-oriented APIs, so that we can switch between different visual representations for the same variables for a better understanding of the dataset.

Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used for detecting the outlier in the data set. It captures the summary of the data efficiently with a simple box and whiskers and allows us to compare easily across groups. Boxplot summarizes sample data using 25th, 50th, and 75th percentiles. These percentiles are also known as the lower quartile, median and upper quartile.

In this article, we will discuss how to order a boxplot manually.

Dataset Used

The dataset used in the below example is https://www.kaggle.com/ranjeetjain3/seaborn-tips-dataset

Step-by-step Approach:

  • Import libraries

Python3






# import required modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
  • Load dataset

Python3




# load dataset
tips= sns.load_dataset('tips')
  
# display top most rows
tips.head()                      

Output:

  • Plot the boxplot.

Python3




# illustrate box plot
fx = sns.boxplot(x='day', y='total_bill', data=tips, hue='sex', palette='Set2')

Output:

  • Plotting the boxplot using seaborn. See the difference in the order of the above figure and after setting the order as per our needs. Palette will change the color of the graph (you can try Set1 and Set3 as well)

Python3




# illustraing box plot with order
fx = sns.boxplot(x='day', y='total_bill', data=tips, order=[
                 'Sun', 'Sat', 'Fri', 'Thur'], hue='sex', palette='Set2')

Output:



  • Both graphs together:

Below is the complete program based on the above approach:

Example 1

Python3




# import required modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
  
  
# load dataset
tips= sns.load_dataset('tips')
  
  
# display top most rows
tips.head()                      
  
  
# illustraing box plot with order
sns.boxplot(x='day', y='total_bill', data=tips, order=[
                 'Sun', 'Sat', 'Fri', 'Thur'], hue='sex', palette='Set2')

Output:

Example 2

Now, Plotting the boxplot using different features. Observe the order on the x-axis in the figure given below:  

Python3




# import required modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
  
  
# load dataset
tips = sns.load_dataset('tips')
  
  
# display top most rows
tips.head()
  
  
# plotting the boxplot taking time on x-axis
fx = sns.boxplot(x="time", y="total_bill", hue="smoker",
                 data=tips, palette="Set1")
  
# illustrating box plot with order
ax = sns.boxplot(x="time", y="total_bill", hue="smoker", order=['Dinner', 'Lunch'],
                 data=tips, palette="Set1")

Output:

  • Before-

  • After-

Here we have manually ordered the boxplot.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :