KDE Plot
Annual Temperature Distribution
KDE showing daily temperature distribution over a year with heat colors.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(44)
# Simulate annual temperatures (bimodal for seasons)
winter = np.random.normal(5, 8, 150)
summer = np.random.normal(28, 6, 150)
spring_fall = np.random.normal(16, 5, 65)
temps = np.concatenate([winter, summer, spring_fall])
kde = stats.gaussian_kde(temps)
x = np.linspace(-15, 45, 500)
y = kde(x)
colors = ['#1e3a8a', '#3b82f6', '#22d3ee', '#fbbf24', '#f97316', '#dc2626']
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')
# Color based on temperature value (cold=blue, hot=red)
for i in range(len(x)-1):
temp_normalized = (x[i] + 15) / 60 # Normalize -15 to 45 range
color_idx = int(temp_normalized * (len(colors) - 1))
color_idx = max(0, min(color_idx, len(colors)-1))
ax.fill_between(x[i:i+2], y[i:i+2], alpha=0.8, color=colors[color_idx])
ax.plot(x, y, color='white', linewidth=2.5)
# Comfort zone
ax.axvspan(18, 24, alpha=0.2, color='#22d3ee', label='Comfort Zone')
ax.axvline(np.mean(temps), color='#fbbf24', linestyle='--', linewidth=2, label=f'Mean: {np.mean(temps):.1f}°C')
ax.set_xlabel('Temperature (°C)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Annual Temperature Distribution', fontsize=16, color='white', fontweight='bold', pad=15)
ax.tick_params(colors='white', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#334155')
ax.legend(loc='upper right', facecolor='#1e293b', edgecolor='#334155', labelcolor='white')
ax.grid(True, alpha=0.1, color='white')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕