Area Chart

QQ Plot with Envelope

Normal QQ plot with simulation envelope - R style

Output
QQ Plot with Envelope
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(42)
n = 100
data = np.random.normal(0, 1, n) + 0.3 * np.random.standard_t(3, n)  # Slightly heavy-tailed

# Theoretical quantiles
theoretical = stats.norm.ppf(np.linspace(0.01, 0.99, n))
sample = np.sort(data)

# Simulation envelope
n_sim = 200
envelope = []
for _ in range(n_sim):
    sim_data = np.random.normal(0, 1, n)
    envelope.append(np.sort(sim_data))
envelope = np.array(envelope)
env_low = np.percentile(envelope, 2.5, axis=0)
env_high = np.percentile(envelope, 97.5, axis=0)

fig, ax = plt.subplots(figsize=(8, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

ax.fill_between(theoretical, env_low, env_high, alpha=0.25, color='#27D3F5', label='95% Envelope')
ax.plot([-3, 3], [-3, 3], color='#F5276C', linewidth=2, linestyle='--', label='Reference line')
ax.scatter(theoretical, sample, color='#4927F5', s=30, alpha=0.7, edgecolors='white', linewidth=0.3)

ax.set_xlabel('Theoretical Quantiles', color='#1f2937', fontsize=11, fontweight='500')
ax.set_ylabel('Sample Quantiles', color='#1f2937', fontsize=11, fontweight='500')
ax.set_title('Normal Q-Q Plot with Simulation Envelope', color='#1f2937', fontsize=13, fontweight='600', pad=15)
ax.tick_params(colors='#374151', labelsize=9)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#e5e7eb')
ax.spines['bottom'].set_color('#e5e7eb')
ax.xaxis.grid(True, color='#f3f4f6', linewidth=0.8)
ax.yaxis.grid(True, color='#f3f4f6', linewidth=0.8)
ax.set_xlim(-3, 3)
ax.set_ylim(-3.5, 3.5)
ax.set_aspect('equal')
ax.legend(facecolor='#ffffff', edgecolor='#e5e7eb', labelcolor='#1f2937')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Basic Charts

Did this help you?

Support PyLucid to keep it free & growing

Support