Violin Plot

Asymmetric Violin Plot

Before/after comparison with different distribution shapes

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

# Data - different distributions
np.random.seed(42)
metrics = ['Latency', 'Throughput', 'Error Rate', 'CPU Load']
baseline = [np.random.exponential(1, 300) + 2,
            np.random.normal(75, 8, 300),
            np.random.exponential(0.5, 300),
            np.random.normal(60, 12, 300)]
optimized = [np.random.exponential(0.6, 300) + 1,
             np.random.normal(85, 5, 300),
             np.random.exponential(0.2, 300),
             np.random.normal(45, 8, 300)]

# Colors
color_base = '#EF4444'
color_opt = '#10B981'

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

positions = range(len(metrics))

# Plot baseline (left side)
for i, d in enumerate(baseline):
    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_base)
        body.set_edgecolor('white')
        body.set_alpha(0.7)

# Plot optimized (right side)
for i, d in enumerate(optimized):
    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_opt)
        body.set_edgecolor('white')
        body.set_alpha(0.7)

# Legend
ax.scatter([], [], c=color_base, s=100, label='Baseline', marker='s')
ax.scatter([], [], c=color_opt, s=100, label='Optimized', marker='s')
ax.legend(loc='upper right', frameon=False, fontsize=11)

# Customize axes
ax.set_xticks(positions)
ax.set_xticklabels(metrics, fontsize=11, fontweight='500')
ax.set_ylabel('Normalized 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