Ridgeline Plot

Stock Returns Distribution by Quarter

Quarterly stock return distributions showing market volatility

Output
Stock Returns Distribution by Quarter
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(42)
quarters = ['Q1 2022', 'Q2 2022', 'Q3 2022', 'Q4 2022', 'Q1 2023', 'Q2 2023', 'Q3 2023', 'Q4 2023']
volatilities = [0.02, 0.035, 0.04, 0.03, 0.025, 0.02, 0.03, 0.025]
means = [0.01, -0.02, -0.01, 0.02, 0.015, 0.02, 0.01, 0.025]

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

colors = ['#F5276C', '#F54927', '#F5B027', '#6CF527', '#27F5B0', '#27D3F5', '#276CF5', '#4927F5']

x = np.linspace(-0.15, 0.15, 200)
overlap = 1.8

for i, (quarter, vol, mean, color) in enumerate(zip(quarters, volatilities, means, colors)):
    data = np.random.normal(mean, vol, 1000)
    kde = stats.gaussian_kde(data)
    y = kde(x) * 0.08
    y_offset = i * overlap
    
    ax.fill_between(x, y_offset, y + y_offset, alpha=0.85, color=color, edgecolor='white', linewidth=0.8)
    ax.axvline(0, color='#333333', linewidth=0.5, linestyle='--', alpha=0.5)
    ax.text(-0.17, y_offset + 0.3, quarter, fontsize=9, color='white', va='center', ha='right', fontweight='500')

ax.set_xlim(-0.20, 0.15)
ax.set_ylim(-0.5, len(quarters) * overlap + 2)
ax.set_xlabel('Daily Returns', color='white', fontsize=11, fontweight='500')
ax.set_title('Stock Returns Distribution by Quarter', color='white', fontsize=14, fontweight='bold', pad=20)
ax.tick_params(colors='#888888', labelsize=9)
ax.set_yticks([])
for spine in ax.spines.values():
    spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['bottom'].set_color('#333333')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support