KDE Plot
Smartphone Battery Life Distribution
KDE comparing battery life across device models.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(58)
flagship = np.random.normal(11, 2, 400)
midrange = np.random.normal(9, 1.5, 500)
budget = np.random.normal(7, 2, 300)
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')
x = np.linspace(2, 18, 500)
devices = [
(flagship, 'Flagship', '#D3F527'),
(midrange, 'Mid-range', '#27D3F5'),
(budget, 'Budget', '#F527B0'),
]
for data, label, color in devices:
kde = stats.gaussian_kde(data)
y = kde(x)
mean_val = np.mean(data)
ax.fill_between(x, y, alpha=0.3, color=color)
ax.plot(x, y, color=color, linewidth=2.5, label=label + ' (' + str(round(mean_val, 1)) + 'h avg)')
ax.plot(x, y, color=color, linewidth=6, alpha=0.15)
ax.axvline(8, color='#F5B027', linestyle='--', linewidth=2, alpha=0.7)
ax.text(8.2, 0.22, 'Heavy Use Threshold', color='#F5B027', fontsize=9, fontweight='bold')
ax.set_xlabel('Battery Life (hours)', fontsize=12, color='white', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='white', fontweight='500')
ax.set_title('Smartphone Battery Life by Tier', 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')
ax.set_xlim(2, 18)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕