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.
pip install mplhepVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Python environment to version 3.9 or newer. Python 3.8 reached its official End-Of-Life (EOL) in October 2024.
Upgrade mplhep to version `1.0.0.rc8` or newer, which replaces the deprecated method with `ax.sharex()` or `ax.sharey()`. If upgrading is not immediately possible, manually adjust axis sharing using `ax.sharex()` / `ax.sharey()` or `ax.get_shared_x_axes().join()` workaround, although the latter is deprecated.
Consult the `mplhep` documentation for `v1.0.0` to understand the updated API, particularly for label handling and the new comparison plotting functions.
Consider migrating to `hep.hist()` for new histogram plotting code if you prefer a `plt.hist()`-like interface, or continue using `hep.histplot()` which is designed for pre-binned histogram data.
Install mplhep and its associated data package using pip: `pip install mplhep mplhep-data`
Ensure `mplhep-data` is installed in your environment: `pip install mplhep-data`
Avoid directly passing `linestyle` as a top-level keyword argument to `mplhep.histplot` when `histtype='errorbar'`. Instead, rely on `mplhep`'s default styling or consult the Matplotlib `errorbar` documentation for how to control line styles for its components (e.g., `fmt` or `caplines` properties).
Explicitly set `histtype='step'` or `histtype='fill'` in your `mplhep.histplot` call to ensure a continuous histogram outline. For proper rendering, especially if providing raw bin contents and edges, ensure the data is structured correctly and rely on the default `edges=True` behavior, which is usually correct for continuous histograms in recent `mplhep` versions.