Bar Chart

Dual Ended Comparison

Back-to-back horizontal bars for year comparison.

Output
Dual Ended Comparison
Python
import matplotlib.pyplot as plt
import numpy as np

COLORS = {
    'left': '#3B82F6',
    'right': '#10B981',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
}

categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
left_vals = [75, 60, 85, 45, 70]
right_vals = [65, 80, 55, 90, 60]

y = np.arange(len(categories))

fig, ax = plt.subplots(figsize=(10, 5), dpi=100)
ax.set_facecolor(COLORS['background'])
fig.patch.set_facecolor(COLORS['background'])

# Left bars (negative direction)
ax.barh(y, [-v for v in left_vals], height=0.5, color=COLORS['left'], alpha=0.85,
        edgecolor='white', linewidth=2, label='2023')

# Right bars
ax.barh(y, right_vals, height=0.5, color=COLORS['right'], alpha=0.85,
        edgecolor='white', linewidth=2, label='2024')

# Center labels
for i, cat in enumerate(categories):
    ax.text(0, i, cat, ha='center', va='center', fontsize=10,
            fontweight='bold', color=COLORS['text'],
            bbox=dict(boxstyle='round,pad=0.3', facecolor='white', edgecolor='none'))

ax.axvline(0, color=COLORS['text'], linewidth=1)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)

ax.set_xlim(-100, 100)
ax.set_xticks([-80, -40, 0, 40, 80])
ax.set_xticklabels(['80', '40', '0', '40', '80'])
ax.tick_params(axis='x', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)
ax.tick_params(axis='y', left=False, labelleft=False)

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.1),
          ncol=2, frameon=False, fontsize=9, labelcolor=COLORS['text_muted'])

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support