Violin Plot

Horizontal Violin Plot

Horizontal orientation for better category label readability

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

# Data
np.random.seed(42)
categories = ['Category A', 'Category B', 'Category C', 'Category D', 'Category E']
data = [np.random.normal(loc, 0.8, 150) for loc in [4, 6, 5, 7, 5.5]]

# Colors
colors = ['#06B6D4', '#0EA5E9', '#3B82F6', '#6366F1', '#8B5CF6']

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

# Horizontal violin plot
vp = ax.violinplot(data, positions=range(len(categories)), widths=0.7,
                   showmeans=True, showmedians=False, showextrema=False, vert=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 mean lines
vp['cmeans'].set_color('#1F2937')
vp['cmeans'].set_linewidth(2)

# Customize axes
ax.set_yticks(range(len(categories)))
ax.set_yticklabels(categories, fontsize=11, fontweight='500')
ax.set_xlabel('Distribution', 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.xaxis.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