Ridgeline Plot

Customer Age by Product Category

Age demographics across different product categories

Output
Customer Age by Product Category
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats

np.random.seed(42)
products = ['Electronics', 'Fashion', 'Home & Garden', 'Sports', 'Books', 'Toys']
age_means = [32, 28, 42, 35, 38, 35]
age_stds = [10, 8, 12, 11, 14, 8]

fig, ax = plt.subplots(figsize=(12, 8), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

colors = ['#4927F5', '#276CF5', '#27D3F5', '#6CF527', '#F5B027', '#F5276C']

x = np.linspace(15, 70, 200)
overlap = 2.2

for i, (product, mean, std, color) in enumerate(zip(products, age_means, age_stds, colors)):
    data = np.random.normal(mean, std, 1000)
    data = np.clip(data, 18, None)
    kde = stats.gaussian_kde(data)
    y = kde(x) * 8
    y_offset = i * overlap
    
    ax.fill_between(x, y_offset, y + y_offset, alpha=0.85, color=color, edgecolor='#374151', linewidth=0.8)
    ax.text(12, y_offset + 0.3, product, fontsize=10, color='#1f2937', va='center', ha='right', fontweight='500')

ax.set_xlim(-5, 70)
ax.set_ylim(-0.5, len(products) * overlap + 2)
ax.set_xlabel('Customer Age', color='#1f2937', fontsize=11, fontweight='500')
ax.set_title('Customer Age Distribution by Product Category', color='#1f2937', fontsize=14, fontweight='bold', pad=20)
ax.tick_params(colors='#374151', labelsize=9)
ax.set_yticks([])
for spine in ax.spines.values():
    spine.set_visible(False)
ax.spines['bottom'].set_visible(True)
ax.spines['bottom'].set_color('#e5e7eb')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support