Raincloud Plot
Diet Type BMI Distribution Raincloud
Comparing BMI distributions across different dietary patterns.
Output
Python
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
np.random.seed(1212)
# BMI values
standard = np.random.normal(27.5, 4.5, 100)
mediterranean = np.random.normal(24.2, 3.2, 90)
keto = np.random.normal(25.8, 3.8, 85)
vegan = np.random.normal(23.5, 2.8, 75)
standard = np.clip(standard, 18, 40)
mediterranean = np.clip(mediterranean, 18, 35)
keto = np.clip(keto, 18, 38)
vegan = np.clip(vegan, 17, 32)
F_stat, p_value = stats.f_oneway(standard, mediterranean, keto, vegan)
BG_COLOR = "#ffffff"
COLOR_SCALE = ["#9ca3af", "#6CF527", "#F5B027", "#27D3F5"]
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
y_data = [standard, mediterranean, keto, vegan]
positions = [0, 1, 2, 3]
labels = ["Standard\nWestern", "Mediterranean", "Ketogenic", "Vegan"]
for h in [20, 25, 30, 35]:
ax.axhline(h, color='#e5e7eb', ls=(0, (5, 5)), alpha=0.8, zorder=0)
# BMI categories
ax.axhspan(18.5, 25, alpha=0.08, color='#22c55e')
ax.axhspan(25, 30, alpha=0.05, color='#fbbf24')
ax.axhspan(30, 42, alpha=0.05, color='#ef4444')
ax.text(3.55, 22, "Normal", color='#22c55e', fontsize=8, va='center')
ax.text(3.55, 27.5, "Overweight", color='#fbbf24', fontsize=8, va='center')
ax.text(3.55, 35, "Obese", color='#ef4444', fontsize=8, va='center')
violins = ax.violinplot(y_data, positions=positions, widths=0.5,
bw_method="silverman", showmeans=False,
showmedians=False, showextrema=False)
for pc in violins["bodies"]:
pc.set_facecolor("none")
pc.set_edgecolor("#374151")
pc.set_linewidth(1.8)
bp = ax.boxplot(y_data, positions=positions, showfliers=False, showcaps=False,
medianprops=dict(linewidth=3, color='#1f2937'),
whiskerprops=dict(linewidth=2, color='#9ca3af'),
boxprops=dict(linewidth=2, color='#9ca3af'))
for i, (y, color) in enumerate(zip(y_data, COLOR_SCALE)):
x_jitter = np.array([i] * len(y)) + stats.t(df=6, scale=0.04).rvs(len(y))
ax.scatter(x_jitter, y, s=50, color=color, alpha=0.5, zorder=2)
means = [y.mean() for y in y_data]
for i, (mean, color) in enumerate(zip(means, COLOR_SCALE)):
ax.scatter(i, mean, s=180, color='#C82909', zorder=5, edgecolors='white', linewidths=2)
ax.plot([i, i + 0.28], [mean, mean], ls="dashdot", color="#374151", zorder=3, lw=1.5)
ax.text(i + 0.3, mean, f"μ={mean:.1f}", fontsize=10, va="center", color='#1f2937',
bbox=dict(facecolor='white', edgecolor=color, boxstyle="round,pad=0.15", lw=2))
# Best diet annotation
ax.annotate('Lowest BMI', xy=(3, vegan.mean()), xytext=(3.3, 20),
fontsize=10, color='#22c55e', ha='left',
arrowprops=dict(arrowstyle='->', color='#22c55e', lw=1.5))
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["left"].set_color('#d1d5db')
ax.spines["bottom"].set_color('#d1d5db')
ax.tick_params(colors='#374151', length=0)
xlabels_full = [f"{l}\n(n={len(y_data[i])})" for i, l in enumerate(labels)]
ax.set_xticks(positions)
ax.set_xticklabels(xlabels_full, size=10, color='#1f2937')
ax.set_ylabel("BMI (kg/m²)", size=14, color='#1f2937', fontweight='bold')
ax.set_title("BMI Distribution by Diet Type", fontsize=14, color="white", fontweight="bold", pad=20)
ax.set_ylim(15, 42)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Raincloud Plot examples
☕