Violin Plot

Classic Violin Plot

Clean violin plot showing distribution shapes across groups

Output
Classic Violin Plot
Python
import matplotlib.pyplot as plt
import numpy as np

# Data
np.random.seed(42)
data = [np.random.normal(loc, 1.2, 200) for loc in [3, 5, 4, 6]]
labels = ['Group A', 'Group B', 'Group C', 'Group D']

# Colors
colors = ['#6366F1', '#8B5CF6', '#A855F7', '#D946EF']

# Create figure
fig, ax = plt.subplots(figsize=(10, 6), facecolor='white')

# Violin plot
vp = ax.violinplot(data, positions=[1, 2, 3, 4], widths=0.7,
                   showmeans=False, showmedians=True, showextrema=False)

# Style violins
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.8)

# Style median lines
vp['cmedians'].set_color('white')
vp['cmedians'].set_linewidth(2)

# Customize axes
ax.set_xticks([1, 2, 3, 4])
ax.set_xticklabels(labels, fontsize=11, fontweight='500')
ax.set_ylabel('Value', fontsize=12, fontweight='500', color='#374151')
ax.set_xlim(0.3, 4.7)
ax.set_ylim(-1, 10)

# 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

Did this help you?

Support PyLucid to keep it free & growing

Support