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

How can I create a stacked bar plot in Seaborn?

Creating a stacked bar plot in Seaborn is a simple and effective way to visualize categorical data. This type of plot allows for the comparison of multiple categories within a single bar, making it ideal for displaying proportions or percentages. To create a stacked bar plot in Seaborn, one can use the “barplot” function and specify the variables for the x and y axis, as well as the variable for the color grouping. This will automatically stack the bars and display the relative sizes of each category. Additionally, Seaborn offers customization options such as changing the color palette and adding labels to further enhance the visualization. With its user-friendly interface and versatile capabilities, Seaborn provides a convenient solution for creating stacked bar plots for data analysis and presentation.

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


A stacked bar plot is a type of chart that uses bars divided into a number of sub-bars to visualize the values of multiple variables at once.

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

stacked bar chart 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'],
                   'Morning': [44, 46, 49, 59, 54],
                   'Evening': [33, 46, 50, 49, 60]})

#view DataFrame
df

	Day	Morning	Evening
0	Mon	44	33
1	Tue	46	46
2	Wed	49	50
3	Thur	59	49
4	Fri	54	60

Step 2: Create the Stacked Bar Chart

We can use the following code to create a stacked bar chart to visualize the total customers each day:

import matplotlib.pyplot as plt
import seaborn as sns

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

#create stacked bar chart
df.set_index('Day').plot(kind='bar', stacked=True, color=['steelblue', 'red'])

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 Stacked Bar Chart

The following code shows how to add axis titles, add an overall title, 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 stacked bar chart
df.set_index('Day').plot(kind='bar', stacked=True, color=['steelblue', 'red'])

#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)

stacked bar chart 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

Cite this article

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

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

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

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

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

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

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