How can I create a grouped bar plot in Seaborn? 2

How can I create a grouped bar plot in Seaborn?

To create a grouped bar plot in Seaborn, you will need to use the “barplot” function and specify the data to be plotted, the x and y variables, and the grouping variable. This will result in a bar plot with bars grouped according to the specified variable. Additionally, you can customize the appearance of the plot by adjusting the color palette, labels, and other parameters. Grouped bar plots are useful for comparing multiple categories within a dataset and can be easily created using the built-in functions and options in Seaborn.

Create a Grouped Bar Plot in Seaborn (Step-by-Step)


A grouped bar plot is a type of chart that uses bars grouped together to visualize the values of multiple variables at once.

This tutorial provides a step-by-step example of how to create the following grouped bar plot in Python using the data visualization package:

grouped bar plot in seaborn

Step 1: Create the Data

First, let’s create the following pandas DataFrame that shows the total number of customers that a restaurant receives in the morning and evening from Monday through Friday:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'Day': ['Mon', 'Tue', 'Wed', 'Thur', 'Fri',
                           'Mon', 'Tue', 'Wed', 'Thur', 'Fri'],
                   'Customers': [44, 46, 49, 59, 54,
                                 33, 46, 50, 49, 60],
                   'Time': ['M', 'M', 'M', 'M', 'M',
                            'E', 'E', 'E', 'E', 'E']})

#view DataFrame
df

	Day	Customers Time
0	Mon	44	  M
1	Tue	46	  M
2	Wed	49	  M
3	Thur	59	  M
4	Fri	54	  M
5	Mon	33	  E
6	Tue	46	  E
7	Wed	50	  E
8	Thur	49	  E
9	Fri	60	  E

Step 2: Create the Grouped Bar Chart

We can use the following code to create a grouped bar chart to visualize the total customers each day, grouped by time:

import matplotlib.pyplot as plt
import seaborn as sns

#set seaborn plotting aesthetics
sns.set(style='white')

#create grouped bar chart
sns.barplot(x='Day', y='Customers', hue='Time', data=df) 

The x-axis displays the day of the week and the bars display how many customers visited the restaurant in the morning and evening each day.

Step 3: Customize the Grouped Bar Chart

The following code shows how to add axis titles, add an overall title, change the colors of the bars, and rotate the x-axis labels to make them easier to read:

import matplotlib.pyplot as plt
import seaborn as sns

#set seaborn plotting aesthetics
sns.set(style='white')

#create grouped bar chart
sns.barplot(x='Day', y='Customers', hue='Time', data=df,
            palette=['purple', 'steelblue'])

#add overall title
plt.title('Customers by Time & Day of Week', fontsize=16)

#add axis titles
plt.xlabel('Day of Week')
plt.ylabel('Number of Customers')

#rotate x-axis labels
plt.xticks(rotation=45)

grouped bar plot in seaborn

Note: We set the seaborn style to ‘white’ for this plot, but you can find a complete list of Seaborn plotting aesthetics on .

Additional Resources

How to Create a Stacked Bar Plot in Seaborn

Cite this article

stats writer (2024). How can I create a grouped bar plot in Seaborn?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-create-a-grouped-bar-plot-in-seaborn/

stats writer. "How can I create a grouped bar plot in Seaborn?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-create-a-grouped-bar-plot-in-seaborn/.

stats writer. "How can I create a grouped bar plot in Seaborn?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-create-a-grouped-bar-plot-in-seaborn/.

stats writer (2024) 'How can I create a grouped bar plot in Seaborn?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-create-a-grouped-bar-plot-in-seaborn/.

[1] stats writer, "How can I create a grouped bar plot in Seaborn?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I create a grouped bar plot in Seaborn?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top