How to Create a Diverging Stacked Bar Chart in Matplotlib?

A diverging stacked bar chart displays data that can extend both above and below a baseline (usually zero). This visualization is useful for comparing positive and negative values across categories, such as survey responses, profit/loss data, or demographic comparisons.

Understanding Diverging Stacked Bar Charts

In a diverging stacked bar chart, bars can extend in both directions from a central baseline. The key is using both positive and negative values to create the diverging effect, with stacked segments building upon each other.

Creating a Basic Diverging Stacked Bar Chart

Here's how to create a diverging stacked bar chart with sample data ?

import matplotlib.pyplot as plt
import numpy as np

# Set figure size
plt.figure(figsize=(10, 6))

# Sample data with positive and negative values
categories = ['Group 1', 'Group 2', 'Group 3', 'Group 4', 'Group 5']
men_scores = [20, -35, 30, 35, -27]
women_scores = [25, -32, 34, 20, -25]

# Create bar positions
x_pos = np.arange(len(categories))
width = 0.6

# Create the bars
bars1 = plt.bar(x_pos, men_scores, width, label='Men', alpha=0.8)
bars2 = plt.bar(x_pos, women_scores, width, bottom=men_scores, label='Women', alpha=0.8)

# Add horizontal line at zero
plt.axhline(y=0, color='black', linestyle='-', linewidth=0.8)

# Customize the chart
plt.ylabel('Scores')
plt.title('Diverging Stacked Bar Chart: Scores by Group and Gender')
plt.xticks(x_pos, categories)
plt.legend()
plt.grid(axis='y', alpha=0.3)

plt.tight_layout()
plt.show()

Enhanced Version with Error Bars

You can add error bars to show data variability ?

import matplotlib.pyplot as plt
import numpy as np

# Sample data
categories = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, -35, 30, 35, -27]
women_means = [25, -32, 34, 20, -25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]

# Create positions and width
x_pos = np.arange(len(categories))
width = 0.6

# Create figure and axis
fig, ax = plt.subplots(figsize=(10, 6))

# Create stacked bars with error bars
p1 = ax.bar(x_pos, men_means, width, yerr=men_std, 
           label='Men', alpha=0.8, capsize=5)
p2 = ax.bar(x_pos, women_means, width, bottom=men_means, 
           yerr=women_std, label='Women', alpha=0.8, capsize=5)

# Add zero line
ax.axhline(0, color='grey', linewidth=1)

# Customize chart
ax.set_ylabel('Scores')
ax.set_title('Diverging Stacked Bar Chart with Error Bars')
ax.set_xticks(x_pos)
ax.set_xticklabels(categories)
ax.legend()
ax.grid(axis='y', alpha=0.3)

plt.tight_layout()
plt.show()

Key Components

Component Purpose Parameter
Bottom bars First layer of data plt.bar(x, values1)
Stacked bars Second layer stacked on first bottom=values1
Zero line Visual baseline reference axhline(0)
Error bars Show data uncertainty yerr=std_values

Customization Options

You can customize colors, transparency, and styling ?

import matplotlib.pyplot as plt
import numpy as np

# Data
categories = ['Q1', 'Q2', 'Q3', 'Q4']
positive_vals = [15, -20, 25, -10]
negative_vals = [12, -15, 18, -8]

x_pos = np.arange(len(categories))

# Create chart with custom colors
fig, ax = plt.subplots(figsize=(8, 6))

bars1 = ax.bar(x_pos, positive_vals, 0.6, 
              color='steelblue', alpha=0.8, label='Category A')
bars2 = ax.bar(x_pos, negative_vals, 0.6, bottom=positive_vals,
              color='lightcoral', alpha=0.8, label='Category B')

# Styling
ax.axhline(0, color='black', linewidth=1.2)
ax.set_ylabel('Values')
ax.set_title('Custom Styled Diverging Stacked Bar Chart')
ax.set_xticks(x_pos)
ax.set_xticklabels(categories)
ax.legend(loc='upper right')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

plt.tight_layout()
plt.show()

Conclusion

Diverging stacked bar charts effectively visualize data that spans positive and negative values. Use the bottom parameter to stack bars and include a horizontal line at zero for clear reference. This chart type is particularly useful for survey data, financial comparisons, and any dataset with opposing categories.

---
Updated on: 2026-03-25T23:41:47+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements