Violin Plot
Income Distribution by Education
Annual income distribution showing inequality across education levels
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# Income distribution by education level (log-normal)
np.random.seed(42)
education = ['No Diploma', 'High School', 'Bachelor', 'Master', 'Doctorate']
median_incomes = [28000, 42000, 65000, 82000, 105000]
income_data = [np.random.lognormal(np.log(m), 0.5, 300) for m in median_incomes]
# Clip extreme values for visualization
income_data = [np.clip(d, 10000, 250000) / 1000 for d in income_data]
# Colors (wealth gradient)
colors = ['#94A3B8', '#64748B', '#6366F1', '#4F46E5', '#4338CA']
# Create figure
fig, ax = plt.subplots(figsize=(12, 7), facecolor='white')
vp = ax.violinplot(income_data, positions=range(len(education)), widths=0.8,
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)
# Subtle shadow
path = body.get_paths()[0]
shadow = path.vertices.copy()
shadow[:, 0] += 0.03
shadow[:, 1] -= 1
ax.fill(shadow[:, 0], shadow[:, 1], color='black', alpha=0.05, zorder=0)
# Poverty and median lines
poverty_line = 30 # $30K
ax.axhline(poverty_line, color='#EF4444', linewidth=2, linestyle='--', alpha=0.7)
ax.text(4.6, poverty_line + 2, 'Poverty Line', fontsize=9, color='#EF4444')
# Median and mean markers
for i, inc in enumerate(income_data):
median = np.median(inc)
mean = np.mean(inc)
# Median (diamond)
ax.scatter(i, median, c='white', s=100, marker='D', zorder=10,
edgecolor=colors[i], linewidth=2)
# Mean (triangle)
ax.scatter(i, mean, c=colors[i], s=60, marker='^', zorder=10,
edgecolor='white', linewidth=1)
# Gini approximation (spread indicator)
iqr = np.percentile(inc, 75) - np.percentile(inc, 25)
ax.errorbar(i + 0.35, median, yerr=iqr/2, color='#9CA3AF',
capsize=4, capthick=1.5, linewidth=1.5, alpha=0.6)
# Legend
ax.scatter([], [], c='white', s=80, marker='D', edgecolor='#6366F1',
linewidth=2, label='Median')
ax.scatter([], [], c='#6366F1', s=50, marker='^', edgecolor='white',
linewidth=1, label='Mean')
ax.legend(loc='upper left', frameon=True, facecolor='white',
edgecolor='#E5E7EB', fontsize=10)
# Styling
ax.set_xticks(range(len(education)))
ax.set_xticklabels(education, fontsize=10, fontweight='500', rotation=15, ha='right')
ax.set_ylabel('Annual Income ($K)', fontsize=12, fontweight='500', color='#374151')
ax.set_ylim(0, 200)
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
☕