Violin Plot

Distribution Comparison Violin

Compare different statistical distribution shapes

Output
Distribution Comparison Violin
Python
import matplotlib.pyplot as plt
import numpy as np

# Data - different distribution types
np.random.seed(42)
distributions = {
    'Normal': np.random.normal(5, 1, 500),
    'Skewed Right': np.random.exponential(2, 500) + 1,
    'Skewed Left': 10 - np.random.exponential(2, 500),
    'Bimodal': np.concatenate([np.random.normal(3, 0.7, 250),
                               np.random.normal(7, 0.7, 250)]),
    'Uniform': np.random.uniform(2, 8, 500)
}

labels = list(distributions.keys())
data = list(distributions.values())

# Colors
colors = ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6']

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

# Violin plot
vp = ax.violinplot(data, positions=range(len(data)), widths=0.7,
                   showmeans=True, showmedians=True, 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)

vp['cmeans'].set_color('#1F2937')
vp['cmeans'].set_linewidth(2)
vp['cmeans'].set_linestyle('--')
vp['cmedians'].set_color('white')
vp['cmedians'].set_linewidth(2.5)

# Add horizontal reference line at median of Normal
ax.axhline(y=5, color='#9CA3AF', linestyle=':', linewidth=1.5, alpha=0.7)
ax.text(4.7, 5.2, 'Reference', fontsize=9, color='#6B7280')

# Customize axes
ax.set_xticks(range(len(data)))
ax.set_xticklabels(labels, fontsize=10, fontweight='500')
ax.set_ylabel('Value', 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

Did this help you?

Support PyLucid to keep it free & growing

Support