Violin Plot

Violin with Data Points

Violin plot with individual data points and mean markers

Output
Violin with Data Points
Python
import matplotlib.pyplot as plt
import numpy as np

# Data
np.random.seed(42)
data = [np.random.normal(4, 0.8, 50),
        np.random.normal(6, 1.2, 50),
        np.random.normal(5, 1.0, 50)]
labels = ['Control', 'Treatment A', 'Treatment B']

# Colors
colors = ['#6366F1', '#10B981', '#F59E0B']

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

positions = [1, 2, 3]

# Violin plot
vp = ax.violinplot(data, positions=positions, 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.4)

# Scatter points with jitter
for i, (d, pos) in enumerate(zip(data, positions)):
    jitter = np.random.uniform(-0.12, 0.12, len(d))
    ax.scatter(pos + jitter, d, c=colors[i], s=30, alpha=0.7,
               edgecolor='white', linewidth=0.5, zorder=3)
    
    # Mean marker
    ax.scatter(pos, np.mean(d), c='white', s=120, marker='D',
               edgecolor=colors[i], linewidth=2, zorder=4)

# Customize axes
ax.set_xticks(positions)
ax.set_xticklabels(labels, fontsize=11, fontweight='500')
ax.set_ylabel('Response Value', fontsize=12, fontweight='500', color='#374151')
ax.set_xlim(0.3, 3.7)

# 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