Area Chart

Time Series Components

Trend and seasonal decomposition - R style

Output
Time Series Components
Python
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)
t = np.arange(0, 365*2)  # 2 years daily

# Components
trend = 100 + t * 0.05
seasonal = 15 * np.sin(2 * np.pi * t / 365)
residual = np.random.normal(0, 5, len(t))
observed = trend + seasonal + residual

fig, axes = plt.subplots(3, 1, figsize=(10, 8), facecolor='#0a0a0f', sharex=True)

for ax in axes:
    ax.set_facecolor('#0a0a0f')
    ax.tick_params(colors='#888888', labelsize=9)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_color('#333333')
    ax.spines['bottom'].set_color('#333333')

# Observed
axes[0].fill_between(t, trend.min(), observed, alpha=0.4, color='#27D3F5')
axes[0].plot(t, observed, color='#27D3F5', linewidth=1)
axes[0].set_ylabel('Observed', color='white', fontsize=10)

# Trend
axes[1].fill_between(t, trend.min(), trend, alpha=0.5, color='#F5276C')
axes[1].plot(t, trend, color='#F5276C', linewidth=2)
axes[1].set_ylabel('Trend', color='white', fontsize=10)

# Seasonal
axes[2].fill_between(t, 0, seasonal, alpha=0.5, color='#6CF527')
axes[2].plot(t, seasonal, color='#6CF527', linewidth=1.5)
axes[2].axhline(0, color='#333333', linewidth=0.5)
axes[2].set_ylabel('Seasonal', color='white', fontsize=10)
axes[2].set_xlabel('Day', color='white', fontsize=11)

axes[0].set_title('Time Series Decomposition', color='white', fontsize=13, fontweight='600', pad=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support