KDE Plot
Vehicle Fuel Efficiency Distribution
KDE comparing fuel efficiency across vehicle types.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
np.random.seed(103)
sedan = np.random.normal(35, 6, 400)
suv = np.random.normal(25, 5, 350)
truck = np.random.normal(20, 4, 250)
fig, ax = plt.subplots(figsize=(12, 6), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
x = np.linspace(5, 55, 500)
vehicles = [
(sedan, 'Sedan', '#6CF527'),
(suv, 'SUV', '#F5B027'),
(truck, 'Truck', '#F5276C'),
]
for data, label, color in vehicles:
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, 0)) + ' mpg)')
ax.axvline(30, color='#27D3F5', linestyle='--', linewidth=2, label='EPA Target')
ax.set_xlabel('Fuel Efficiency (MPG)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_ylabel('Density', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Vehicle Fuel Efficiency by Type', fontsize=16, color='#1f2937', fontweight='bold', pad=15)
ax.tick_params(colors='#374151', labelsize=10)
for spine in ax.spines.values():
spine.set_color('#d1d5db')
ax.legend(loc='upper right', facecolor='#f9fafb', edgecolor='#d1d5db', labelcolor='#374151')
ax.grid(True, alpha=0.3, color='#e5e7eb')
ax.set_xlim(5, 55)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
☕