Area Chart

Autocorrelation Function

ACF plot with confidence bounds - R style

Output
Autocorrelation Function
Python
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(42)
n = 200
data = np.cumsum(np.random.normal(0, 1, n))  # Random walk

# Compute ACF
lags = 30
acf = np.array([np.corrcoef(data[:-lag], data[lag:])[0,1] if lag > 0 else 1 for lag in range(lags)])
ci = 1.96 / np.sqrt(n)

fig, ax = plt.subplots(figsize=(10, 6), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')

# CI band
ax.fill_between(range(lags), -ci, ci, alpha=0.2, color='#27D3F5')
ax.axhline(ci, color='#27D3F5', linewidth=1, linestyle='--')
ax.axhline(-ci, color='#27D3F5', linewidth=1, linestyle='--')
ax.axhline(0, color='#888888', linewidth=1)

# ACF bars
colors = ['#6CF527' if abs(a) <= ci else '#F5276C' for a in acf]
ax.bar(range(lags), acf, color=colors, alpha=0.8, width=0.3)

ax.set_xlabel('Lag', color='white', fontsize=11, fontweight='500')
ax.set_ylabel('ACF', color='white', fontsize=11, fontweight='500')
ax.set_title('Autocorrelation Function', color='white', fontsize=13, fontweight='600', pad=15)
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')
ax.set_xlim(-0.5, lags-0.5)
ax.set_ylim(-0.3, 1.1)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support