2D Histogram

Basketball Shot Analysis

Sports analytics visualization showing shot distance vs success probability with player tracking data.

Output
Basketball Shot Analysis
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# Basketball data
np.random.seed(42)
n_shots = 8000

distance = np.random.exponential(10, n_shots) + 3
distance = np.clip(distance, 0, 30)

shot_quality = np.random.beta(2, 2, n_shots) * 100

# Sports court theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#0c0810')
ax.set_facecolor('#0c0810')

# Custom colormap - basketball orange
colors = ['#0c0810', '#181020', '#241830', '#302040', '#402850', 
          '#603060', '#803870', '#a04080', '#c05090', '#e070a0', '#ff90b0']
cmap = LinearSegmentedColormap.from_list('basketball', colors, N=256)

# 2D histogram
h = ax.hist2d(distance, shot_quality, bins=[40, 40], cmap=cmap, cmin=1)

# 3-point line
ax.axvline(x=23.75, color='#ff6b35', linestyle='--', alpha=0.8, linewidth=2.5, label='3-Point Line')
ax.axvline(x=15, color='#ffa502', linestyle=':', alpha=0.6, linewidth=1.5, label='Mid-Range')

# Hot zone
from matplotlib.patches import Rectangle
hot = Rectangle((0, 70), 8, 30, fill=False, edgecolor='#ff90b0', 
                linewidth=2.5, linestyle='-', alpha=0.8, label='Hot Zone')
ax.add_patch(hot)

# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#0c0810')
cbar.set_label('Shot Attempts', fontsize=11, color='#e0a0c0', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#e0a0c0')
cbar.outline.set_edgecolor('#402040')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#e0a0c0', fontsize=9)

ax.set_xlabel('Shot Distance (feet)', fontsize=13, color='#e0a0c0', fontweight='600', labelpad=10)
ax.set_ylabel('Shot Quality Score', fontsize=13, color='#e0a0c0', fontweight='600', labelpad=10)
ax.set_title('NBA Shot Distribution Analysis', fontsize=16, color='white', fontweight='bold', pad=20)

ax.tick_params(colors='#c080a0', labelsize=10, length=0)
for spine in ax.spines.values():
    spine.set_visible(False)

ax.legend(loc='upper right', fontsize=9, facecolor='#1c1020', edgecolor='#402040', labelcolor='#e0a0c0')
plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support