Violin Plot

Violin Density Plot

Intensity-coded violins showing severity levels

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

# Data
np.random.seed(42)
categories = ['Low', 'Medium', 'High', 'Critical']
data = [np.random.exponential(1, 400),
        np.random.normal(3, 0.8, 400),
        np.random.normal(5, 1.2, 400),
        np.random.gamma(5, 1, 400) + 3]

# Color by intensity
base_color = np.array([99, 102, 241]) / 255  # #6366F1
intensities = [0.4, 0.6, 0.8, 1.0]

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

vp = ax.violinplot(data, positions=range(len(data)), widths=0.75,
                   showmeans=False, showmedians=True, showextrema=False)

for i, body in enumerate(vp['bodies']):
    color = base_color * intensities[i] + (1 - intensities[i]) * np.array([1, 1, 1])
    body.set_facecolor(color)
    body.set_edgecolor('white')
    body.set_linewidth(2)
    body.set_alpha(0.9)

vp['cmedians'].set_color('white')
vp['cmedians'].set_linewidth(2.5)

# Add count labels
for i, d in enumerate(data):
    ax.text(i, ax.get_ylim()[1] - 0.5, f'n={len(d)}',
            ha='center', fontsize=10, color='#6B7280')

# Customize axes
ax.set_xticks(range(len(categories)))
ax.set_xticklabels(categories, fontsize=11, fontweight='500')
ax.set_ylabel('Response Time (s)', 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