Raincloud Plot
EV Battery Degradation Raincloud
Comparing battery capacity retention across electric vehicle manufacturers.
Output
Python
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
np.random.seed(321)
# Battery capacity retention % after 5 years
tesla = np.random.normal(92, 3, 80)
rivian = np.random.normal(89, 4, 65)
ford = np.random.normal(85, 5, 75)
gm = np.random.normal(83, 6, 70)
tesla = np.clip(tesla, 80, 100)
rivian = np.clip(rivian, 75, 98)
ford = np.clip(ford, 68, 95)
gm = np.clip(gm, 65, 95)
F_stat, p_value = stats.f_oneway(tesla, rivian, ford, gm)
BG_COLOR = "#0d1117"
COLOR_SCALE = ["#F5276C", "#F5B027", "#276CF5", "#6CF527"]
fig, ax = plt.subplots(figsize=(10, 6), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)
y_data = [tesla, rivian, ford, gm]
positions = [0, 1, 2, 3]
labels = ["Tesla", "Rivian", "Ford", "GM"]
for h in [70, 80, 90]:
ax.axhline(h, color='#333333', ls=(0, (5, 5)), alpha=0.5, zorder=0)
# Warranty threshold
ax.axhline(y=70, color='#ef4444', ls='--', alpha=0.8, lw=2)
ax.text(3.55, 70, "Warranty", color='#ef4444', fontsize=9, 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("#27D3F5")
pc.set_linewidth(2)
bp = ax.boxplot(y_data, positions=positions, showfliers=False, showcaps=False,
medianprops=dict(linewidth=3, color='white'),
whiskerprops=dict(linewidth=2, color='#555555'),
boxprops=dict(linewidth=2, color='#555555'))
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=55, color=color, alpha=0.55, 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="white", zorder=3, lw=1.5)
ax.text(i + 0.3, mean, f"μ={mean:.1f}%", fontsize=10, va="center", color='white',
bbox=dict(facecolor=BG_COLOR, edgecolor=color, boxstyle="round,pad=0.15", lw=2))
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["left"].set_color('#444444')
ax.spines["bottom"].set_color('#444444')
ax.tick_params(colors='#888888', length=0)
xlabels = [f"{l}\n(n={len(y_data[i])})" for i, l in enumerate(labels)]
ax.set_xticks(positions)
ax.set_xticklabels(xlabels, size=12, color='white')
ax.set_ylabel("Battery Capacity (%)", size=12, color='white', fontweight='bold')
ax.set_title("EV Battery Degradation Analysis", fontsize=14, color='white', fontweight='bold', pad=15)
stats_text = f"ANOVA: F={F_stat:.1f}, p<0.001, ω²=0.58"
bbox = dict(facecolor=BG_COLOR, edgecolor='#F5276C', boxstyle="round,pad=0.3", lw=2)
ax.text(0.5, 1.02, stats_text, transform=ax.transAxes, fontsize=10, color='white',
ha='center', va='bottom', fontfamily='monospace', bbox=bbox)
ax.set_ylim(62, 102)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Raincloud Plot examples
☕