KDE Plot
Stock Return Distribution KDE
Kernel density estimation of daily stock returns comparing market sectors
Output
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
np.random.seed(123)
BG_COLOR = '#0d1117'
TEXT_COLOR = 'white'
# Daily returns (%)
tech = np.random.normal(0.08, 1.8, 500)
finance = np.random.normal(0.05, 1.2, 500)
healthcare = np.random.normal(0.06, 0.9, 500)
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
x_range = np.linspace(-6, 6, 500)
for data, color, label in [(tech, '#4927F5', 'Technology'),
(finance, '#F5B027', 'Finance'),
(healthcare, '#27F5B0', 'Healthcare')]:
kde = gaussian_kde(data)
density = kde(x_range)
ax.plot(x_range, density, color=color, linewidth=2.5, label=label)
ax.fill_between(x_range, density, alpha=0.25, color=color)
ax.axvline(0, color='#ef4444', linestyle='--', alpha=0.7, label='Zero Return')
ax.set_xlabel('Daily Return (%)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Density', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Stock Return Distribution by Sector', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors='#888888', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#333333')
ax.legend(facecolor=BG_COLOR, edgecolor='#333333', labelcolor=TEXT_COLOR, fontsize=10)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕