Creating Stacked Bar Plots with Seaborn to Matplotlib: A Step-by-Step Guide

Creating Stacked Bar Plots with Seaborn to Matplotlib

Seaborn is a powerful visualization library in Python that extends matplotlib to make statistical graphics more aesthetically appealing. One of its key features is the ability to create stacked bar plots, which can be particularly useful for comparing categorical data across different groups. In this article, we will explore how to create a stacked bar plot from Seaborn to Matplotlib.

Introduction to Stacked Bar Plots

A stacked bar plot is a type of bar chart where multiple series are stacked on top of each other. Each series in the plot represents a different category or group, and the height of each bar corresponds to the value of that category. The key advantage of stacked bar plots is that they allow for easy comparison between categories across different groups.

Creating a Stacked Bar Plot with Seaborn

Seaborn’s countplot function can be used to create a stacked bar plot. The dodge=False parameter is essential in creating a stacked plot, as it places all bars belonging to the same airline one on top of the other.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns

sns.set()
np.random.seed(123)
trafic = pd.DataFrame({'airlines': np.random.choice([*'abcdefghij'], 500),
                       'typecode': np.random.choice([*'qrstuvwxyz'], 500)})

fig = plt.figure(figsize=(10, 5))
ax = sns.countplot(x="airlines", hue='typecode', palette='rocket', dodge=False, data=trafic,
                   order=trafic.airlines.value_counts(ascending=False).iloc[:5].index,
                   hue_order=trafic.typecode.value_counts(ascending=False).iloc[:5].index)
ax.set(xlabel="Airlines code", ylabel='Count')

Modifying the Plot to Create a Stacked Bar Chart

To create a stacked bar plot, we need to modify the code slightly. We can use the containers attribute of the axis object to access each bar in the plot.

bottoms = {}
for bars in ax.containers:
    for bar in bars:
        x, y = bar.get_xy()
        h = bar.get_height()
        if x in bottoms:
            bar.set_y(bottoms[x])
            bottoms[x] += h
        else:
            bottoms[x] = h

ax.relim()  # the plot limits need to be updated with the moved bars
ax.autoscale()

Explanation of the Code

The bottoms dictionary is used to keep track of the bottom height of each bar. When a new bar is added, its height is compared with the existing bottom height at the same x-coordinate. If they are equal, the new bar is placed on top of the existing one; otherwise, it is placed below.

Example Use Case

Suppose we have a dataset that shows the number of sales for different products in various regions. We can use a stacked bar plot to compare the sales across different regions and products.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns

sns.set()
np.random.seed(123)
data = pd.DataFrame({'Product': ['A', 'B', 'C'], 'Region1': [10, 20, 30], 'Region2': [40, 50, 60]})

fig = plt.figure(figsize=(8, 6))
ax = sns.barplot(x='Product', y=['Region1', 'Region2'], data=data)
ax.set_xlabel('Product')
ax.set_ylabel('Sales')

# Create a stacked bar plot
bottoms = {}
for bars in ax.containers:
    for bar in bars:
        x, y = bar.get_xy()
        h = bar.get_height()
        if x in bottoms:
            bar.set_y(bottoms[x])
            bottoms[x] += h
        else:
            bottoms[x] = h

ax.relim()  # the plot limits need to be updated with the moved bars
ax.autoscale()

plt.show()

Conclusion

In this article, we explored how to create a stacked bar plot from Seaborn to Matplotlib. We used the countplot function with the dodge=False parameter and modified the plot to create a stacked bar chart. This technique is particularly useful for comparing categorical data across different groups.


Last modified on 2023-09-21