Table of Contents
Countplot is a visualization tool provided by Seaborn library to represent the count of categorical data. In order to order the bars in a countplot based on count values, one can use the “order” parameter in the countplot function. This parameter takes in a list of categories and arranges the bars in the specified order. Alternatively, one can also use the “hue” parameter to group the data by a certain category and then use the “order” parameter to order the bars within each group. This allows for a more organized and informative representation of the count data in the countplot.
Seaborn countplot: Order Bars by Count
You can use the following basic syntax to order the bars in a seaborn countplot in descending order:
sns.countplot(data=df, x='var', order=df['var'].value_counts().index)
To order the bars in ascending order, simply add ascending=True in the value_counts() function:
sns.countplot(data=df, x='var', order=df['var'].value_counts(ascending=True).index)
The following examples show how to use this syntax in practice with the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'C', 'C', 'C', 'D', 'D'], 'points': [12, 11, 18, 15, 14, 20, 25, 24, 32, 30]}) #view DataFrame print(df) team points 0 A 12 1 A 11 2 A 18 3 A 15 4 B 14 5 C 20 6 C 25 7 C 24 8 D 32 9 D 30
Example 1: Create Seaborn countplot with Bars in Default Order
The following code shows how to create a seaborn countplot in which the bars are in the default order (i.e. the order in which the unique values appear in the column):
import seaborn as sns
#create countplot to visualize occurrences of unique values in 'team' column
sns.countplot(data=df, x='team')

Notice that the bars in the plot are simply ordered based on the order in which the unique values appear in the team column.
Example 2: Create Seaborn countplot with Bars in Descending Order
The following code shows how to create a seaborn countplot in which the bars are in descending order:
import seaborn as sns #create countplot with values in descending order sns.countplot(data=df, x='team', order=df['team'].value_counts().index)

Notice that the bars in the plot are now in descending order.
Example 3: Create Seaborn countplot with Bars in Ascending Order
import seaborn as sns #create countplot with values in ascending order sns.countplot(data=df, x='team', order=df['team'].value_counts(ascending=True).index)

Notice that the bars in the plot are now in ascending order.
Note: You can find the complete documentation for the seaborn countplot() function .
The following tutorials explain how to perform other common functions in seaborn:
Cite this article
stats writer (2024). How can I order the bars in a countplot generated by Seaborn based on count values?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-order-the-bars-in-a-countplot-generated-by-seaborn-based-on-count-values/
stats writer. "How can I order the bars in a countplot generated by Seaborn based on count values?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-order-the-bars-in-a-countplot-generated-by-seaborn-based-on-count-values/.
stats writer. "How can I order the bars in a countplot generated by Seaborn based on count values?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-order-the-bars-in-a-countplot-generated-by-seaborn-based-on-count-values/.
stats writer (2024) 'How can I order the bars in a countplot generated by Seaborn based on count values?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-order-the-bars-in-a-countplot-generated-by-seaborn-based-on-count-values/.
[1] stats writer, "How can I order the bars in a countplot generated by Seaborn based on count values?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I order the bars in a countplot generated by Seaborn based on count values?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
