Sort Boxplot by Mean with Seaborn in Python Last Updated : 01 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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 to the data structures from pandas.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 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. Sometimes, we want to order the boxplots according to our needs there are many ways you can order a boxplot that are: Order of boxplot manuallySorting of boxplot using mean In this article, we will discuss how to order a boxplot using mean. What sort boxplot using mean? When we have multiple groups it's suggested to use sorting by mean or median manually it will get difficult to sort. Step-by-step Approach:Importing Libraries Python3 # import required modules import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt Creating dataset Python3 # creating dataset df = pd.DataFrame({ 'Ice-cream': np.random.normal(57, 5, 100), 'Chocolate': np.random.normal(73, 5, 100), 'cupcake': np.random.normal(68, 8, 100), 'jamroll': np.random.normal(37, 10, 100), 'cake': np.random.normal(76, 5, 100), }) df.head() Output: Plot the data before sorting the boxplot. Python3 # plot the data into boxplot sns.boxplot(data=df) # Label x-axis plt.xlabel('Desserts') # labels y-axis plt.ylabel('preference of people') Output: Now sort the data first and get the sorted indices as we have to sort the boxplot using mean, so we will apply the mean() and sort_values function to the data. Python3 # This will give the indices of the sorted # values into the ascending order the default # value in sort_values is ascending = True index_sort = df.mean().sort_values().index index_sort Output: Using sorted index we can sort the data frame that we created. Python3 # now applying the sorted # indices to the data df_sorted = df[index_sort] So We have sorted the data let's plot the boxplot of the data. Python3 # plotting the boxplot for the data sns.boxplot(data = df_sorted) # Label x-axis plt.xlabel('Desserts') # labels y-axis plt.ylabel('preference of people') Output: If one wants to sort in descending order then use the below syntax: index_sort = df.mean().sort_values(ascending=False).index Below is the complete program based on the above approach: Python3 # import required modules import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt # creating dataset df = pd.DataFrame({ 'Ice-cream': np.random.normal(57, 5, 100), 'Chocolate': np.random.normal(73, 5, 100), 'cupcake': np.random.normal(68, 8, 100), 'jamroll': np.random.normal(37, 10, 100), 'cake': np.random.normal(76, 5, 100), }) # sort on the basis of mean index_sort = df.mean().sort_values().index # now applying the sorted indices to the data df_sorted = df[index_sort] # plotting the boxplot for the data sns.boxplot(data = df_sorted) # Label x-axis plt.xlabel('Desserts') # labels y-axis plt.ylabel('preference of people') Output: Comment More infoAdvertise with us Next Article Sort Boxplot by Mean with Seaborn in Python P prachibindal2925 Follow Improve Article Tags : Python Python-Seaborn Practice Tags : python Similar Reads How to Show Mean on Boxplot using Seaborn in Python? A boxplot is a powerful data visualization tool used to understand the distribution of data. It splits the data into quartiles, and summarises it based on five numbers derived from these quartiles: median: the middle value of data. marked as Q2, portrays the 50th percentile.first quartile: the middl 2 min read Boxplot using Seaborn in Python Boxplot is used to see the distribution of numerical data and identify key stats like minimum and maximum values, median, identifying outliers, understanding how data is distributed and can compare the distribution of data across different categories or variables. In Seaborn the seaborn.boxplot() fu 3 min read Grouped Boxplots in Python with Seaborn Boxplot depicts the distribution of quantitative data facilitating comparisons between different variables, continuous or categorical. It is a common data dispersion measure. Boxplots consist of a five-number summary which helps in detecting and removing outliers from the dataset. Minimum observatio 2 min read Horizontal Boxplots with Seaborn in Python Prerequisite: seaborn The Boxplots are used to visualize the distribution of data which is useful when a comparison of data is required. Sometimes, Boxplot is also known as a box-and-whisker plot. The box shows the quartiles of dataset and whiskers extend to show rest of the distribution. In this ar 1 min read Grouped Barplots in Python with Seaborn Prerequisites: Seaborn In this article, we will discuss ways to make grouped barplots using Seaborn in Python. Before that, there are some concepts one must be familiar with: Barcharts: Barcharts are great when you have two variables one is numerical and therefore the other may be a categorical vari 2 min read Barplot using seaborn in Python 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 to the data structures from pandas. Se 6 min read Seaborn.barplot() method in Python Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. There is just something extraordinary about a well-designed visualization. The colors stand out, the layers blend nicely together, the c 4 min read Python - seaborn.boxenplot() method Prerequisite : Fundamentals of Seaborn Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. There is just something extraordinary about a well-designed visualization. The colors stand out, 3 min read How to Make ECDF Plot with Seaborn in Python? Prerequisites:  Seaborn In this article, we are going to make the ECDF plot with Seaborn Library. ECDF PlotECDF stands for Empirical Commutative Distribution. It is more likely to use instead of the histogram for visualizing the data because the ECDF plot visualizes each and every data point of the 5 min read How to Make Grouped Violinplot with Seaborn in Python? This article depicts how to make a grouped violinplot with Seaborn in python. Violinplot is a great way of visualizing the data as a combination of the box plot with the kernel density plots to produce a new type of plot. For this article, we will be using the iris dataset to plot data. This comes 3 min read Like