Matplotlib styles for HEP

1.1.2 · active · verified Tue Apr 14

mplhep is a Python library providing Matplotlib styles and utility functions tailored for High Energy Physics (HEP) plotting. It simplifies the creation of plots compatible with various LHC experiments (ATLAS, CMS, LHCb, ALICE) and offers tools for 1D and 2D histograms, including comparison plots. The library is actively maintained, with the current version being 1.1.2, and sees regular patch and minor releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import `mplhep`, apply an experiment-specific style (CMS in this case), plot a 1D histogram using `hep.histplot`, and add an experiment label with luminosity and year information. It uses `numpy` for data generation and `matplotlib.pyplot` for basic plot setup and display.

import matplotlib.pyplot as plt
import mplhep as hep
import numpy as np

# Set the CMS experiment style
hep.style.use("CMS")

# Generate some dummy data for a histogram
data = np.random.normal(loc=5, scale=2, size=1000)
bins = np.linspace(0, 10, 20)
hist, edges = np.histogram(data, bins=bins)

# Create a figure and axes
fig, ax = plt.subplots()

# Plot the histogram using mplhep's histplot
hep.histplot(hist, edges, ax=ax, label="My Data")

# Add experiment label (e.g., CMS)
hep.cms.label(loc=0, data=True, lumi=20, year=2018, ax=ax)

# Add a legend and show the plot
ax.legend()
ax.set_xlabel("X-axis [GeV]")
ax.set_ylabel("Entries")
plt.tight_layout()
plt.show()

view raw JSON →