How To Manually Order Boxplot in Seaborn? - GeeksforGeeks

04 Oct, 2021
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.
The dataset used in the below example is https://www.kaggle.com/ranjeetjain3/seaborn-tips-dataset
Import libraries
Python3
# import required modulesimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns
  
Load dataset
Python3
# load datasettips= sns.load_dataset('tips') # display top most rowstips.head()                     
Output:
Plot the boxplot.
Python3
# illustrate box plotfx = 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
# illustrating box plot with orderfx = 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 modulesimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns  # load datasettips= sns.load_dataset('tips')  # display top most rowstips.head()                       # illustrating box plot with ordersns.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 modulesimport pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns  # load datasettips = sns.load_dataset('tips')  # display top most rowstips.head()  # plotting the boxplot taking time on x-axisfx = sns.boxplot(x="time", y="total_bill", hue="smoker",                 data=tips, palette="Set1") # illustrating box plot with orderax = 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.
sweetyty
Python-Seaborn
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Adding new column to existing DataFrame in Pandas
How to get column names in Pandas dataframe
Python Dictionary
Python map() function
Taking input in Python
Defaultdict in Python
Read JSON file using Python
Enumerate() in Python
Iterate over a list in Python
Different ways to create Pandas Dataframe