Event Plot

Basketball Game Events

Play-by-play event visualization across quarters

Output
Basketball Game Events
Python
import matplotlib.pyplot as plt
import numpy as np

# Simulate basketball game events
np.random.seed(42)
event_types = ['3-Pointers', '2-Pointers', 'Free Throws', 'Turnovers', 'Fouls']
game_events = [
    np.sort(np.random.uniform(0, 48, np.random.randint(8, 15))),
    np.sort(np.random.uniform(0, 48, np.random.randint(25, 40))),
    np.sort(np.random.uniform(0, 48, np.random.randint(15, 25))),
    np.sort(np.random.uniform(0, 48, np.random.randint(10, 18))),
    np.sort(np.random.uniform(0, 48, np.random.randint(15, 25))),
]

# Event colors
colors = ['#8B5CF6', '#3B82F6', '#10B981', '#EF4444', '#F59E0B']

# Create figure
fig, ax = plt.subplots(figsize=(12, 6), facecolor='white')

for i, (events, color) in enumerate(zip(game_events, colors)):
    ax.eventplot(events, lineoffsets=i, linelengths=0.6, linewidths=2.5,
                 colors=color, alpha=0.8)
    
    # Event count
    ax.text(49, i, f'{len(events)}', fontsize=11, va='center',
            color=color, fontweight='bold')

# Quarter markers
for q in [12, 24, 36]:
    ax.axvline(q, color='#D1D5DB', linewidth=2, linestyle='-')
ax.text(6, 4.7, 'Q1', ha='center', fontsize=10, color='#6B7280', fontweight='500')
ax.text(18, 4.7, 'Q2', ha='center', fontsize=10, color='#6B7280', fontweight='500')
ax.text(30, 4.7, 'Q3', ha='center', fontsize=10, color='#6B7280', fontweight='500')
ax.text(42, 4.7, 'Q4', ha='center', fontsize=10, color='#6B7280', fontweight='500')

# Styling
ax.set_yticks(range(len(event_types)))
ax.set_yticklabels(event_types, fontsize=11, fontweight='500')
ax.set_xlabel('Game Time (minutes)', fontsize=12, fontweight='500', color='#374151')
ax.set_xlim(0, 51)
ax.set_ylim(-0.5, len(event_types) - 0.5)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#E5E7EB')
ax.spines['bottom'].set_color('#E5E7EB')
ax.tick_params(colors='#6B7280', labelsize=10)

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support