KDE Plot
Battery Life Distribution KDE
Density distribution of smartphone battery life by brand
Output
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
np.random.seed(1414)
BG_COLOR = '#ffffff'
TEXT_COLOR = '#1f2937'
GRID_COLOR = '#e5e7eb'
# Screen-on time in hours
brand_a = np.random.normal(8.5, 1.2, 400)
brand_b = np.random.normal(7.8, 1.5, 400)
brand_c = np.random.normal(9.2, 1.0, 400)
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
x_range = np.linspace(4, 14, 500)
for data, color, label in [(brand_a, '#276CF5', 'Brand A'),
(brand_b, '#6CF527', 'Brand B'),
(brand_c, '#F5276C', 'Brand C')]:
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(8, color='#F5B027', linestyle='--', alpha=0.7, linewidth=2)
ax.text(8.1, ax.get_ylim()[1]*0.85, '8hr Target', color='#F5B027', fontsize=10)
ax.set_xlabel('Screen-On Time (hours)', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Density', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Smartphone Battery Life Distribution', fontsize=14, color=TEXT_COLOR, fontweight='bold', pad=15)
ax.tick_params(colors=TEXT_COLOR, labelsize=10)
for spine in ax.spines.values():
spine.set_color(GRID_COLOR)
ax.yaxis.grid(True, color=GRID_COLOR, linewidth=0.5, alpha=0.7)
ax.legend(facecolor=BG_COLOR, edgecolor=GRID_COLOR, labelcolor=TEXT_COLOR, fontsize=10)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕