A set of helpers for matplotlib
to more easily produce plots typically
needed in HEP as well as style them in way that’s compatible with current
collaboration requirements (ROOT-like plots for CMS, ATLAS, LHCb, ALICE).
pip install mplhep
A tutorial given at PyHEP 2020 is available as a binder here or you can watch the recording here.
Documentation can be found at mplhep.readthedocs.io.
import mplhep as hep
hep.style.use(hep.style.ROOT) # For now ROOT defaults to CMS
# Or choose one of the experiment styles
hep.style.use(hep.style.ATLAS)
# or
hep.style.use("CMS") # string aliases work too
# {"ALICE" | "ATLAS" | "CMS" | "LHCb1" | "LHCb2"}
Or use matplotlib
API directly
plt.style.use(hep.style.ROOT)
If the default styles are not what you need, please open an issue.
Default experiment labels are also available.
# Overall - both left and right annotation
hep.<experiment>.label(<text>, data=<True|False>, lumi=50, year=2017)
# Just experiment label and <text> such as 'Preliminary' or 'Simulation'
hep.<experiment>.text(<text>)
You can use loc={0..5}
to control the label positioning.
h, bins = [2, 3, 2], [0, 1, 2, 3]
hep.histplot(h, bins)
import numpy as np
xbins, ybins = [0, 1, 2, 3], [0, 1, 2, 3]
H = np.array([[2,3,2], [1,2,1], [3,1,3]])
hep.hist2dplot(H, xbins, ybins)
hep.savelabels('test.png')
will produces 4 variation on experiment label
hep.savelabels('test', labels=["FOO", "BAR"], suffixes=["foo.pdf", "bar"])
will produce
<experiment>.label()
will remain unchanged.hep.style.use("fira")
- use Fira Sanshep.style.use("firamath")
- use Fira Mathhep.style.use(["CMS", "fira", "firamath"])
rcParams
get overwritten silentlyhep.style.use("CMS")
hep.style.use({"font.sans-serif":'Comic Sans MS'})
hep.style.use("CMSTex")
- Use LaTeX to produce all text labelsAs it is ROOT does not come with any fonts and therefore relies on using system fonts. Therefore the font in a figure can be dependent on whether it was produced on OSX or PC. The default sans-serif font used is Helvetica, but it only comes with OSX, in Windows this will silently fallback to Arial.
Both Helvetica and Arial are proprietary, which as far as fonts go means you can use it to create any text/graphics once you have the license, but you cannot redistribute the font files as part of other software. That means we cannot just package Helvetica with this to make sure everyone has the same font in plots.
Luckily for fonts it seems only the software is copyrighted, not the actual shapes, which means there are quite a few open alternatives with similar look. The most closely resembling Helvetica being Tex Gyre Heros
http://www.gust.org.pl/projects/e-foundry/tex-gyre/heros
You can compare yourself if the differences are meanigful below.
They are Tex Gyre Heros, Helvetica and Arial respectively.
For consistent styling Fira Sans is included as well.
https://github.com/mozilla/Fira
https://github.com/firamath/firamath
with pyplot.style.context(style.ROOT):
plotting...
For now one has to set the style globally:
Updating list of citations and use cases of mplhep
in publications: