Violin Plot

Disease Incubation Periods

Comparative analysis of viral incubation times with quarantine guidelines

Output
Disease Incubation Periods
Python
import matplotlib.pyplot as plt
import numpy as np

# Incubation period data (days)
np.random.seed(42)
diseases = ['COVID-19', 'Influenza', 'Measles', 'Chickenpox', 'Monkeypox']
incubation = [
    np.random.gamma(4, 1.2, 300) + 1,      # COVID: 2-14 days
    np.random.gamma(3, 0.5, 300) + 0.5,    # Flu: 1-4 days
    np.random.gamma(5, 2, 300) + 7,        # Measles: 7-21 days
    np.random.gamma(6, 2.5, 300) + 10,     # Chickenpox: 10-21 days
    np.random.gamma(4, 3, 300) + 5,        # Monkeypox: 5-21 days
]

# Severity colors
colors = ['#EF4444', '#F97316', '#8B5CF6', '#3B82F6', '#EC4899']

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

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

for i, body in enumerate(vp['bodies']):
    body.set_facecolor(colors[i])
    body.set_edgecolor('white')
    body.set_linewidth(2)
    body.set_alpha(0.75)
    
    # Shadow effect
    path = body.get_paths()[0]
    shadow = path.vertices.copy()
    shadow[:, 0] += 0.04
    shadow[:, 1] -= 0.3
    ax.fill(shadow[:, 0], shadow[:, 1], color='black', alpha=0.05)

# Quarantine reference periods
quarantine_periods = [14, 5, 21, 21, 21]
for i, q in enumerate(quarantine_periods):
    ax.scatter(i, q, marker='_', s=400, c='#1F2937', linewidth=3, zorder=10)

# Add IQR and median
for i, inc in enumerate(incubation):
    q1, median, q3 = np.percentile(inc, [25, 50, 75])
    
    # IQR box
    ax.fill_between([i - 0.08, i + 0.08], q1, q3, 
                    color=colors[i], alpha=0.6, zorder=5)
    ax.scatter(i, median, c='white', s=80, zorder=10,
               edgecolor=colors[i], linewidth=2)
    
    # Peak annotation
    ax.text(i, median + 2, f'{median:.1f}d', ha='center', fontsize=9,
            color='#374151', fontweight='600')

# Legend
ax.scatter([], [], marker='_', s=200, c='#1F2937', linewidth=3, 
           label='Quarantine Period')
ax.legend(loc='upper right', frameon=True, facecolor='white',
          edgecolor='#E5E7EB', fontsize=10)

# Styling
ax.set_xticks(range(len(diseases)))
ax.set_xticklabels(diseases, fontsize=11, fontweight='600')
ax.set_ylabel('Incubation Period (days)', fontsize=12, fontweight='500', color='#374151')
ax.set_ylim(0, 35)

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