Violin Plot
Split Violin Plot
Side-by-side comparison of two groups within each category
Output
Python
import matplotlib.pyplot as plt
import numpy as np
# Data
np.random.seed(42)
categories = ['Q1', 'Q2', 'Q3', 'Q4']
group_a = [np.random.normal(5, 1, 200), np.random.normal(6, 1.2, 200),
np.random.normal(5.5, 0.9, 200), np.random.normal(7, 1.1, 200)]
group_b = [np.random.normal(4, 1.1, 200), np.random.normal(5, 1, 200),
np.random.normal(6, 1.3, 200), np.random.normal(6.5, 0.8, 200)]
# Colors
color_a = '#6366F1'
color_b = '#F59E0B'
# Create figure
fig, ax = plt.subplots(figsize=(10, 6), facecolor='white')
positions = np.arange(len(categories))
# Plot left half (Group A)
for i, d in enumerate(group_a):
vp = ax.violinplot([d], positions=[i], widths=0.8,
showmeans=False, showmedians=False, showextrema=False)
for body in vp['bodies']:
m = np.mean(body.get_paths()[0].vertices[:, 0])
body.get_paths()[0].vertices[:, 0] = np.clip(
body.get_paths()[0].vertices[:, 0], -np.inf, m)
body.set_facecolor(color_a)
body.set_edgecolor('white')
body.set_alpha(0.8)
# Plot right half (Group B)
for i, d in enumerate(group_b):
vp = ax.violinplot([d], positions=[i], widths=0.8,
showmeans=False, showmedians=False, showextrema=False)
for body in vp['bodies']:
m = np.mean(body.get_paths()[0].vertices[:, 0])
body.get_paths()[0].vertices[:, 0] = np.clip(
body.get_paths()[0].vertices[:, 0], m, np.inf)
body.set_facecolor(color_b)
body.set_edgecolor('white')
body.set_alpha(0.8)
# Legend
ax.scatter([], [], c=color_a, s=100, label='Group A', marker='s')
ax.scatter([], [], c=color_b, s=100, label='Group B', marker='s')
ax.legend(loc='upper right', frameon=False, fontsize=10)
# Customize axes
ax.set_xticks(positions)
ax.set_xticklabels(categories, fontsize=11, fontweight='500')
ax.set_ylabel('Performance Score', fontsize=12, fontweight='500', color='#374151')
ax.set_ylim(0, 11)
# 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
☕