2D Histogram

Particle Detection Density

Signal vs background event distribution in detector

Output
Particle Detection Density
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap

# Particle physics detector data
np.random.seed(42)
n_events = 12000

# Energy (GeV) and transverse momentum
energy = np.random.exponential(50, n_events) + 10
energy = np.clip(energy, 10, 500)

pt = np.random.exponential(20, n_events)
pt = np.clip(pt, 1, 200)

# Physics dark theme
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(10, 8))
fig.patch.set_facecolor('#050508')
ax.set_facecolor('#050508')

# Custom colormap - detector orange-yellow
colors = ['#050508', '#101018', '#1a1a28', '#2a2a38', '#3a3a48', 
          '#5a4a48', '#7a5a48', '#aa6a40', '#da8a30', '#ffaa20', '#ffdd10']
cmap = LinearSegmentedColormap.from_list('detector', colors, N=256)

# 2D histogram
h = ax.hist2d(energy, pt, bins=[50, 45], cmap=cmap, cmin=1)

# Signal region
from matplotlib.patches import Rectangle
signal = Rectangle((100, 50), 150, 80, fill=False, edgecolor='#ffaa20', 
                    linewidth=2, linestyle='--', alpha=0.8)
ax.add_patch(signal)
ax.text(175, 95, 'Signal Region', fontsize=10, color='#ffaa20', ha='center', alpha=0.9)

# Styled colorbar
cbar = plt.colorbar(h[3], ax=ax, pad=0.02, shrink=0.85)
cbar.ax.set_facecolor('#050508')
cbar.set_label('Event Count', fontsize=11, color='#e0c080', labelpad=10)
cbar.ax.yaxis.set_tick_params(color='#e0c080')
cbar.outline.set_edgecolor('#3a3a28')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#e0c080', fontsize=9)

# Labels
ax.set_xlabel('Energy (GeV)', fontsize=13, color='#e0c080', fontweight='600', labelpad=10)
ax.set_ylabel('Transverse Momentum (GeV/c)', fontsize=13, color='#e0c080', fontweight='600', labelpad=10)
ax.set_title('Particle Detector Event Distribution', fontsize=16, color='white', fontweight='bold', pad=20)

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

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

Statistical

Did this help you?

Support PyLucid to keep it free & growing

Support