Violin Plot
Violin with Quartiles
Violin plot with explicit quartile markers and IQR
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# Data
np.random.seed(42)
groups = ['Segment A', 'Segment B', 'Segment C', 'Segment D']
data = [np.random.gamma(3, 1, 300) + 2,
np.random.normal(6, 1.5, 300),
np.random.exponential(2, 300) + 3,
np.random.normal(5, 2, 300)]
# Colors
colors = ['#8B5CF6', '#6366F1', '#3B82F6', '#0EA5E9']
# Create figure
fig, ax = plt.subplots(figsize=(10, 6), facecolor='white')
vp = ax.violinplot(data, positions=range(len(data)), widths=0.7,
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(1.5)
body.set_alpha(0.7)
# Add quartile lines
for i, d in enumerate(data):
q1, median, q3 = np.percentile(d, [25, 50, 75])
# Quartile lines
ax.hlines([q1, q3], i - 0.15, i + 0.15, color=colors[i], linewidth=2)
# Median line (thicker)
ax.hlines(median, i - 0.2, i + 0.2, color='white', linewidth=3)
ax.hlines(median, i - 0.18, i + 0.18, color=colors[i], linewidth=2)
# IQR connecting line
ax.vlines(i, q1, q3, color=colors[i], linewidth=2)
# Customize axes
ax.set_xticks(range(len(groups)))
ax.set_xticklabels(groups, fontsize=11, fontweight='500')
ax.set_ylabel('Value Distribution', fontsize=12, fontweight='500', color='#374151')
# Clean styling
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
☕