Violin Plot
Cognitive Function by Age
Working memory assessment showing age-related cognitive changes
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# Cognitive test scores by age group
np.random.seed(42)
age_groups = ['18-25', '26-35', '36-45', '46-55', '56-65', '65+']
# Working memory scores (higher = better)
baseline_scores = [105, 108, 105, 100, 95, 88]
scores = [np.random.normal(base, 12, 80) for base in baseline_scores]
# Age-related color gradient (vibrant to muted)
colors = ['#10B981', '#22C55E', '#84CC16', '#EAB308', '#F97316', '#EF4444']
# Create figure
fig, ax = plt.subplots(figsize=(12, 7), facecolor='white')
vp = ax.violinplot(scores, positions=range(len(age_groups)), widths=0.75,
showmeans=False, showmedians=False, showextrema=False)
for i, body in enumerate(vp['bodies']):
body.set_facecolor(colors[i])
body.set_edgecolor('white')
body.set_linewidth(2)
body.set_alpha(0.75)
# Glow effect
path = body.get_paths()[0]
ax.fill(path.vertices[:, 0], path.vertices[:, 1],
color=colors[i], alpha=0.15, zorder=0)
# Reference bands
ax.axhspan(90, 110, color='#E0E7FF', alpha=0.4, zorder=0)
ax.text(5.55, 100, 'Average\nRange', fontsize=9, color='#6366F1',
va='center', fontweight='500')
ax.axhline(115, color='#10B981', linewidth=1.5, linestyle=':', alpha=0.7)
ax.text(5.55, 115, 'High', fontsize=9, color='#10B981', va='center')
ax.axhline(85, color='#EF4444', linewidth=1.5, linestyle=':', alpha=0.7)
ax.text(5.55, 85, 'Low', fontsize=9, color='#EF4444', va='center')
# Add statistics
for i, score in enumerate(scores):
median = np.median(score)
pct_above_avg = (score >= 100).mean() * 100
# Median with confidence band
ci_low, ci_high = np.percentile(score, [25, 75])
ax.fill_between([i - 0.1, i + 0.1], ci_low, ci_high,
color=colors[i], alpha=0.4, zorder=5)
ax.scatter(i, median, c='white', s=100, zorder=10,
edgecolor=colors[i], linewidth=2.5)
# Percentage above average
ax.text(i, 130, f'{pct_above_avg:.0f}%', ha='center', fontsize=10,
color=colors[i], fontweight='bold')
# Trend line
medians = [np.median(s) for s in scores]
z = np.polyfit(range(len(age_groups)), medians, 2)
p = np.poly1d(z)
x_smooth = np.linspace(0, 5, 50)
ax.plot(x_smooth, p(x_smooth), color='#6366F1', linewidth=2.5,
linestyle='--', alpha=0.6, zorder=3)
# Labels
ax.text(-0.5, 132, '% Above Avg', fontsize=9, color='#6B7280')
# Styling
ax.set_xticks(range(len(age_groups)))
ax.set_xticklabels(age_groups, fontsize=11, fontweight='600')
ax.set_xlabel('Age Group', fontsize=12, fontweight='500', color='#374151')
ax.set_ylabel('Working Memory Score', fontsize=12, fontweight='500', color='#374151')
ax.set_ylim(50, 140)
ax.set_xlim(-0.5, 5.5)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#E5E7EB')
ax.spines['bottom'].set_color('#E5E7EB')
ax.tick_params(colors='#6B7280', labelsize=10)
ax.yaxis.grid(True, linestyle='--', alpha=0.3, color='#9CA3AF')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Violin Plot examples
☕