Violin Plot
Basketball Player Sprint Analysis
Speed distribution by position with elite threshold markers
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# Performance data by position
np.random.seed(42)
positions = ['Point Guard', 'Shooting Guard', 'Small Forward', 'Power Forward', 'Center']
# Sprint speed (m/s)
speeds = [
np.random.normal(8.2, 0.3, 80), # PG - fastest
np.random.normal(7.9, 0.35, 80), # SG
np.random.normal(7.6, 0.4, 80), # SF
np.random.normal(7.2, 0.45, 80), # PF
np.random.normal(6.8, 0.5, 80), # C - slowest
]
# Team colors
colors = ['#EF4444', '#F97316', '#EAB308', '#22C55E', '#3B82F6']
# Create figure
fig, ax = plt.subplots(figsize=(12, 7), facecolor='#0F172A')
ax.set_facecolor('#0F172A')
vp = ax.violinplot(speeds, positions=range(len(positions)), widths=0.75,
showmeans=False, showmedians=False, showextrema=False)
for i, body in enumerate(vp['bodies']):
body.set_facecolor(colors[i])
body.set_edgecolor(colors[i])
body.set_linewidth(1)
body.set_alpha(0.25)
# Neon glow layers
path = body.get_paths()[0]
for lw, alpha in [(10, 0.08), (6, 0.15), (3, 0.3), (1.5, 0.6)]:
ax.plot(path.vertices[:, 0], path.vertices[:, 1],
color=colors[i], linewidth=lw, alpha=alpha)
# Elite threshold
elite_speed = 7.8
ax.axhline(elite_speed, color='#FBBF24', linewidth=2, linestyle='--', alpha=0.6)
ax.text(4.55, elite_speed + 0.05, 'ELITE', fontsize=9, color='#FBBF24',
fontweight='bold', va='bottom')
# Stats for each position
for i, speed in enumerate(speeds):
median = np.median(speed)
elite_pct = (speed >= elite_speed).mean() * 100
# Glowing median dot
for s, a in [(150, 0.2), (100, 0.4), (60, 0.8)]:
ax.scatter(i, median, c=colors[i], s=s, alpha=a, zorder=10)
ax.scatter(i, median, c='white', s=30, zorder=11)
# Elite percentage
ax.text(i, 5.8, f'{elite_pct:.0f}%', ha='center', fontsize=10,
color='#FBBF24' if elite_pct > 30 else '#64748B', fontweight='bold')
# Styling
ax.set_xticks(range(len(positions)))
ax.set_xticklabels(positions, fontsize=10, fontweight='600', color='white')
ax.set_ylabel('Sprint Speed (m/s)', fontsize=12, fontweight='500', color='#94A3B8')
ax.set_ylim(5.5, 9.5)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#334155')
ax.spines['bottom'].set_color('#334155')
ax.tick_params(colors='#94A3B8', labelsize=10)
ax.yaxis.grid(True, linestyle='--', alpha=0.15, color='#475569')
# Label
ax.text(-0.5, 5.95, '% Elite', fontsize=9, color='#64748B')
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Violin Plot examples
☕