Line & Scatter

Budget Variance Analysis

Actual vs budget with variance highlighting.

Output
Budget Variance Analysis
Python
import matplotlib.pyplot as plt
import numpy as np

# === STYLE CONFIG ===
COLORS = {
    'budget': '#94A3B8',
    'actual': '#1E293B',
    'positive': '#10B981',
    'negative': '#EF4444',
    'background': '#FFFFFF',
    'text': '#1E293B',
    'text_muted': '#64748B',
    'grid': '#F1F5F9',
}

# === DATA ===
categories = ['Q1', 'Q2', 'Q3', 'Q4']
budget = [250, 280, 310, 350]
actual = [265, 275, 340, 360]
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 ===
# Budget baseline
ax.plot(x, budget, color=COLORS['budget'], linewidth=2, linestyle='--',
        marker='o', markersize=10, markerfacecolor='white', markeredgewidth=2,
        label='Budget', zorder=2)

# Actual with variance coloring
ax.plot(x, actual, color=COLORS['actual'], linewidth=2.5, zorder=3)

for i, (b, a) in enumerate(zip(budget, actual)):
    variance = a - b
    color = COLORS['positive'] if variance >= 0 else COLORS['negative']
    
    # Variance area
    ax.fill_between([x[i]-0.1, x[i]+0.1], [b, b], [a, a], 
                    color=color, alpha=0.3)
    
    # Actual point
    ax.scatter([x[i]], [a], color=color, s=100, 
               edgecolors='white', linewidths=2, zorder=4)
    
    # Variance label
    sign = '+' if variance >= 0 else ''
    ax.annotate(f'{sign}{variance}K', xy=(x[i], a),
                xytext=(0, 12), textcoords='offset points',
                ha='center', fontsize=9, fontweight='bold', color=color)

# === AXES ===
ax.set_xlim(-0.5, len(categories) - 0.5)
ax.set_ylim(200, 400)
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.set_xlabel('Quarter', fontsize=10, color=COLORS['text'], labelpad=10)
ax.set_ylabel('Revenue ($K)', fontsize=10, color=COLORS['text'], labelpad=10)

# === 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)
ax.set_axisbelow(True)
ax.tick_params(axis='both', colors=COLORS['text_muted'], labelsize=9, length=0, pad=8)

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

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Pairwise Data

Did this help you?

Support PyLucid to keep it free & growing

Support