ANOVA Violin Plot
Building Energy Consumption ANOVA
Comparing energy usage patterns across different building types.
Output
Python
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
np.random.seed(777)
# Energy consumption in kWh per sq meter per year
residential = np.random.normal(85, 22, 100)
commercial = np.random.normal(165, 35, 100)
industrial = np.random.normal(280, 65, 100)
data_center = np.random.normal(520, 95, 100)
F_stat, p_value = stats.f_oneway(residential, commercial, industrial, data_center)
fig, ax = plt.subplots(figsize=(12, 7), facecolor='#0a0a0f')
ax.set_facecolor('#0a0a0f')
# Energy-themed colors
colors = ['#27F5B0', '#27D3F5', '#F5B027', '#F5276C']
parts = ax.violinplot([residential, commercial, industrial, data_center],
positions=[1, 2, 3, 4], showmeans=True, showmedians=True, widths=0.75)
for i, pc in enumerate(parts['bodies']):
pc.set_facecolor(colors[i])
pc.set_alpha(0.65)
pc.set_edgecolor(colors[i])
pc.set_linewidth(2)
parts['cmeans'].set_color('#F5D327')
parts['cmeans'].set_linewidth(2.5)
parts['cmedians'].set_color('white')
for partname in ['cbars', 'cmins', 'cmaxes']:
parts[partname].set_color('#444444')
# Energy efficiency thresholds
ax.axhline(y=150, color='#6CF527', linestyle=':', alpha=0.7, linewidth=1.5)
ax.text(0.55, 150, 'Efficient', fontsize=9, color='#6CF527', va='bottom')
ax.axhline(y=350, color='#F5276C', linestyle=':', alpha=0.7, linewidth=1.5)
ax.text(0.55, 350, 'High Usage', fontsize=9, color='#F5276C', va='bottom')
labels = ['Residential', 'Commercial', 'Industrial', 'Data Center']
means = [residential.mean(), commercial.mean(), industrial.mean(), data_center.mean()]
# Mean labels
for i, (mean, color) in enumerate(zip(means, colors)):
ax.scatter([i+1], [mean], color=color, s=100, marker='_', linewidths=3, zorder=5)
# CO2 equivalent annotations
co2_factors = [0.42, 0.42, 0.42, 0.42]
for i, (mean, factor, color) in enumerate(zip(means, co2_factors, colors)):
co2 = mean * factor
ax.text(i+1, 10, f'CO₂: {co2:.0f} kg', ha='center', fontsize=8, color=color)
stats_text = f"ANOVA RESULTS\n{'═'*16}\nF = {F_stat:.1f}\np < 0.001\n\nVariance: {p_value:.2e}"
bbox = dict(boxstyle="round,pad=0.5", facecolor='#020B14', edgecolor='#F5B027', lw=2)
ax.text(0.02, 0.98, stats_text, transform=ax.transAxes, fontsize=10, color='white',
va='top', fontfamily='monospace', bbox=bbox)
ax.set_xticks([1, 2, 3, 4])
ax.set_xticklabels(labels, fontsize=11, color='white')
ax.set_ylabel('Energy Use (kWh/m²/year)', fontsize=12, color='white', fontweight='500')
ax.set_title('Energy Consumption by Building Type\nAnnual Usage Intensity Comparison',
fontsize=14, color='white', fontweight='bold', pad=15)
ax.tick_params(colors='#888888')
for spine in ax.spines.values():
spine.set_color('#333333')
ax.yaxis.grid(True, color='#1a1a2e', linewidth=0.5)
ax.set_axisbelow(True)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More ANOVA Violin Plot examples
☕