Violin Plot

Brain Alpha Wave Activity

EEG alpha power comparison between baseline and meditation states

Output
Brain Alpha Wave Activity
Python
import matplotlib.pyplot as plt
import numpy as np

# EEG signal power by brain region
np.random.seed(42)
regions = ['Frontal', 'Temporal', 'Parietal', 'Occipital', 'Central']
# Alpha wave power (8-12 Hz) during meditation
baseline = [np.random.gamma(3, 2, 100) for _ in regions]
meditation = [np.random.gamma(5, 2.5, 100), np.random.gamma(4, 2, 100),
              np.random.gamma(6, 2, 100), np.random.gamma(8, 2, 100),
              np.random.gamma(4.5, 2, 100)]

# Colors
color_baseline = '#94A3B8'
color_meditation = '#8B5CF6'

# Create figure with dark scientific theme
fig, ax = plt.subplots(figsize=(12, 7), facecolor='#1E1B4B')
ax.set_facecolor('#1E1B4B')

positions_base = np.arange(len(regions)) - 0.2
positions_med = np.arange(len(regions)) + 0.2

# Baseline violins
vp1 = ax.violinplot(baseline, positions=positions_base, widths=0.35,
                    showmeans=False, showmedians=False, showextrema=False)
for body in vp1['bodies']:
    body.set_facecolor(color_baseline)
    body.set_edgecolor('white')
    body.set_alpha(0.6)

# Meditation violins with glow
vp2 = ax.violinplot(meditation, positions=positions_med, widths=0.35,
                    showmeans=False, showmedians=False, showextrema=False)
for body in vp2['bodies']:
    body.set_facecolor(color_meditation)
    body.set_edgecolor(color_meditation)
    body.set_alpha(0.7)
    
    # Glow effect
    path = body.get_paths()[0]
    for lw, alpha in [(8, 0.1), (4, 0.2)]:
        ax.plot(path.vertices[:, 0], path.vertices[:, 1],
                color=color_meditation, linewidth=lw, alpha=alpha)

# Add percentage increase annotations
for i in range(len(regions)):
    base_med = np.median(baseline[i])
    med_med = np.median(meditation[i])
    increase = ((med_med - base_med) / base_med) * 100
    
    ax.scatter(positions_base[i], base_med, c='white', s=40, zorder=10)
    ax.scatter(positions_med[i], med_med, c='white', s=40, zorder=10)
    
    # Connection line
    ax.plot([positions_base[i], positions_med[i]], [base_med, med_med],
            color='#A78BFA', linewidth=1.5, linestyle=':', alpha=0.5)
    
    # Increase label
    ax.text(i, max(med_med, base_med) + 4, f'+{increase:.0f}%',
            ha='center', fontsize=10, color='#A78BFA', fontweight='bold')

# Legend
ax.scatter([], [], c=color_baseline, s=100, label='Baseline', marker='s')
ax.scatter([], [], c=color_meditation, s=100, label='Meditation', marker='s')
ax.legend(loc='upper left', frameon=True, facecolor='#2E2A5E', 
          edgecolor='#4C4680', labelcolor='white', fontsize=10)

# Styling
ax.set_xticks(range(len(regions)))
ax.set_xticklabels(regions, fontsize=11, fontweight='600', color='white')
ax.set_ylabel('Alpha Power (μV²)', fontsize=12, fontweight='500', color='#C4B5FD')
ax.set_ylim(0, 45)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#4C4680')
ax.spines['bottom'].set_color('#4C4680')
ax.tick_params(colors='#A5B4FC', labelsize=10)
ax.yaxis.grid(True, linestyle='--', alpha=0.2, color='#6366F1')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support