Violin Plot

Half Violin Plot

Single-sided violin with median lines and mean markers

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

# Data
np.random.seed(42)
categories = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
data = [np.random.normal(loc, 1.1, 200) for loc in [4, 5, 6, 5.5, 7, 6.5]]

# Color gradient
colors = ['#0EA5E9', '#06B6D4', '#14B8A6', '#10B981', '#22C55E', '#84CC16']

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

for i, (d, color) in enumerate(zip(data, colors)):
    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)
        body.set_edgecolor('white')
        body.set_linewidth(1.5)
        body.set_alpha(0.85)
    
    # Add median line
    median = np.median(d)
    ax.hlines(median, i, i + 0.35, color='white', linewidth=2.5)
    
    # Add mean dot
    ax.scatter(i + 0.17, np.mean(d), c='white', s=50, zorder=5,
               edgecolor=color, linewidth=2)

# Customize axes
ax.set_xticks(range(len(categories)))
ax.set_xticklabels(categories, fontsize=11, fontweight='500')
ax.set_ylabel('Monthly Growth', fontsize=12, fontweight='500', color='#374151')
ax.set_xlim(-0.5, len(categories) - 0.3)

# 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