Bar Chart
Diverging Bar Chart
Positive/negative comparison chart.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# === STYLE CONFIG ===
COLORS = {
'positive': '#10B981',
'negative': '#EF4444',
'background': '#FFFFFF',
'text': '#1E293B',
'text_muted': '#64748B',
'grid': '#F1F5F9',
}
# === DATA ===
categories = ['Feature A', 'Feature B', 'Feature C', 'Feature D', 'Feature E', 'Feature F']
values = [35, 25, -15, 40, -30, 20]
colors = [COLORS['positive'] if v >= 0 else COLORS['negative'] for v in values]
y = np.arange(len(categories))
# === FIGURE ===
fig, ax = plt.subplots(figsize=(10, 6), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])
# === PLOT ===
# Glow first
for i, (v, c) in enumerate(zip(values, colors)):
ax.barh(i, v, height=0.6, color=c, alpha=0.15, zorder=1)
# Main bars
bars = ax.barh(y, values, height=0.5, color=colors, alpha=0.85,
edgecolor='white', linewidth=2, zorder=3)
# Center line
ax.axvline(0, color=COLORS['text'], linewidth=1.5, zorder=2)
# Value labels - more offset to avoid overlap
for i, val in enumerate(values):
offset = 4 if val >= 0 else -4
ha = 'left' if val >= 0 else 'right'
ax.text(val + offset, i, f'{val:+d}%', ha=ha, va='center',
fontsize=10, fontweight='bold', color=COLORS['text'])
# === STYLING ===
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_color(COLORS['grid'])
ax.xaxis.grid(True, color=COLORS['grid'], linewidth=1, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='x', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.tick_params(axis='y', colors=COLORS['text'], labelsize=10, length=0, pad=15) # More padding
ax.set_yticks(y)
ax.set_yticklabels(categories)
ax.set_xlim(-55, 60) # Extended limits to avoid overlap
ax.set_xlabel('Change (%)', fontsize=10, color=COLORS['text'], labelpad=10)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Basic Charts
More Bar Chart examples
☕