ANOVA Violin Plot

Surgical Recovery Time ANOVA

Comparing patient recovery durations across different surgical techniques.

Output
Surgical Recovery Time ANOVA
Python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

np.random.seed(1212)

# Recovery time in days
open_surgery = np.random.lognormal(2.8, 0.4, 100)
laparoscopic = np.random.lognormal(2.3, 0.35, 100)
robotic = np.random.lognormal(2.1, 0.3, 100)

# Clip for realistic values
open_surgery = np.clip(open_surgery, 5, 60)
laparoscopic = np.clip(laparoscopic, 3, 40)
robotic = np.clip(robotic, 2, 30)

F_stat, p_value = stats.f_oneway(open_surgery, laparoscopic, robotic)

fig, ax = plt.subplots(figsize=(11, 7), facecolor='#ffffff')
ax.set_facecolor('#ffffff')

colors = ['#C82909', '#F5B027', '#27F5B0']

parts = ax.violinplot([open_surgery, laparoscopic, robotic], 
                       positions=[1, 2, 3], showmeans=True, showmedians=True, widths=0.7)

for i, pc in enumerate(parts['bodies']):
    pc.set_facecolor(colors[i])
    pc.set_alpha(0.6)
    pc.set_edgecolor(colors[i])
    pc.set_linewidth(2)

parts['cmeans'].set_color('#4927F5')
parts['cmeans'].set_linewidth(2.5)
parts['cmedians'].set_color('#1f2937')
for partname in ['cbars', 'cmins', 'cmaxes']:
    parts[partname].set_color('#9ca3af')

# Target recovery line
ax.axhline(y=14, color='#22c55e', linestyle='--', alpha=0.7, linewidth=2)
ax.text(3.35, 14, 'Target', fontsize=9, color='#22c55e', va='center')

labels = ['Open Surgery', 'Laparoscopic', 'Robotic-Assisted']
means = [open_surgery.mean(), laparoscopic.mean(), robotic.mean()]

# Complication rate at bottom
complication_rates = ['8.2%', '3.5%', '2.1%']
for i, (rate, color, mean) in enumerate(zip(complication_rates, colors, means)):
    ax.text(i+1, -5, f'Comp:{rate} | μ={mean:.1f}d', ha='center', fontsize=9, color=color, fontweight='bold')

# Significance bracket
y_max = 62
ax.plot([1, 1, 3, 3], [y_max, y_max+2, y_max+2, y_max], color='#374151', lw=1.5)
ax.text(2, y_max+3, '***', ha='center', fontsize=12, color='#F5276C', fontweight='bold')

# Stats at top
stats_text = f"ANOVA: F={F_stat:.2f}, p={p_value:.2e} | Fastest: Robotic"
bbox = dict(boxstyle="round,pad=0.3", facecolor='#f0fdf4', edgecolor='#27F5B0', lw=2)
ax.text(0.5, 1.02, stats_text, transform=ax.transAxes, fontsize=10, color='#1f2937',
        ha='center', va='bottom', fontfamily='monospace', bbox=bbox)

ax.set_xticks([1, 2, 3])
ax.set_xticklabels(labels, fontsize=11, color='#1f2937')
ax.set_ylabel('Recovery Time (Days)', fontsize=12, color='#1f2937', fontweight='500')
ax.set_title('Post-Surgical Recovery by Technique\nCholecystectomy Procedure Comparison', 
             fontsize=14, color='#1f2937', fontweight='bold', pad=25)

ax.tick_params(colors='#374151')
for spine in ax.spines.values():
    spine.set_color('#e5e7eb')
ax.yaxis.grid(True, color='#f3f4f6', linewidth=0.8)
ax.set_axisbelow(True)
ax.set_ylim(-10, 72)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support