Mirror Chart

Home vs Away Game Attendance

Mirror histogram comparing sports attendance with revenue analysis

Output
Home vs Away Game Attendance
Python
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

np.random.seed(1981)
BG_COLOR = '#0a0a0f'
TEXT_COLOR = 'white'

home = np.random.normal(42000, 8000, 500)
away = np.random.normal(28000, 10000, 500)
home = home[(home > 10000) & (home < 70000)]
away = away[(away > 5000) & (away < 60000)]

fig, ax = plt.subplots(figsize=(12, 7), facecolor=BG_COLOR)
ax.set_facecolor(BG_COLOR)

x = np.linspace(5000, 70000, 400)

kde_h = gaussian_kde(home)
y_h = kde_h(x)
ax.fill_between(x, y_h, alpha=0.3, color='#F5B027')
ax.plot(x, y_h, color='#F5B027', linewidth=4, alpha=0.3)
ax.plot(x, y_h, color='#F5B027', linewidth=2, label='Home Games')

kde_a = gaussian_kde(away)
y_a = kde_a(x) * -1
ax.fill_between(x, y_a, alpha=0.3, color='#27D3F5')
ax.plot(x, y_a, color='#27D3F5', linewidth=4, alpha=0.3)
ax.plot(x, y_a, color='#27D3F5', linewidth=2, label='Away Games')

ax.axhline(0, color='#333', linewidth=1.5)

med_h = np.median(home)
med_a = np.median(away)
ax.axvline(med_h, color='#F5B027', linestyle='--', linewidth=1.5, alpha=0.7)
ax.axvline(med_a, color='#27D3F5', linestyle='--', linewidth=1.5, alpha=0.7)

home_advantage = (med_h - med_a) / med_a * 100
stats_text = 'Home Advantage:\n+%.0f%% avg attendance\n+%.0fK fans' % (home_advantage, (med_h - med_a)/1000)
ax.text(0.02, 0.98, stats_text.replace('\n', chr(10)), transform=ax.transAxes, fontsize=10,
        color=TEXT_COLOR, verticalalignment='top', fontfamily='monospace',
        bbox=dict(boxstyle='round,pad=0.5', facecolor='#1a1a2e', edgecolor='#333', alpha=0.9))

ax.set_xlabel('Attendance', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_ylabel('Density', fontsize=12, color=TEXT_COLOR, fontweight='500')
ax.set_title('Stadium Attendance: Home vs Away Games', fontsize=14, 
             color=TEXT_COLOR, fontweight='bold', pad=15)

ax.tick_params(colors='#888', labelsize=10)
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '%.0fK' % (x/1000)))
for spine in ax.spines.values():
    spine.set_color('#333')

ax.legend(loc='upper right', facecolor='#1a1a2e', edgecolor='#333', 
          labelcolor=TEXT_COLOR, fontsize=10)
ax.set_xlim(5000, 70000)
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support