Bar Chart

Stacked Bar Chart

Revenue breakdown by department.

Output
Stacked Bar Chart
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'stack1': '#6366F1',
    'stack2': '#10B981',
    'stack3': '#F59E0B',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
categories = ['North', 'South', 'East', 'West']
sales = [40, 35, 45, 30]
services = [25, 30, 20, 25]
support = [15, 20, 15, 20]

x = 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 ===
ax.bar(x, sales, width=0.5, color=COLORS['stack1'], alpha=0.85,
       edgecolor='white', linewidth=1.5, label='Sales', zorder=3)
ax.bar(x, services, width=0.5, bottom=sales, color=COLORS['stack2'], alpha=0.85,
       edgecolor='white', linewidth=1.5, label='Services', zorder=3)
ax.bar(x, support, width=0.5, bottom=np.array(sales)+np.array(services), 
       color=COLORS['stack3'], alpha=0.85, edgecolor='white', linewidth=1.5, 
       label='Support', zorder=3)

# Total labels - well above bars
totals = np.array(sales) + np.array(services) + np.array(support)
for i, total in enumerate(totals):
    ax.text(i, total + 3, f'{total}', ha='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_color(COLORS['grid'])
ax.spines['bottom'].set_color(COLORS['grid'])

ax.yaxis.grid(True, color=COLORS['grid'], linewidth=1, zorder=0)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)

ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.set_ylim(0, 100)  # Room for labels
ax.set_ylabel('Revenue ($M)', fontsize=10, color=COLORS['text'], labelpad=10)

ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.12),
          ncol=3, 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