Matplotlib styles for HEP
Project description
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).
Installation
pip install mplhep
Getting Started
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.
Styling
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.
Plotting
1D Histograms
h, bins = [2, 3, 2], [0, 1, 2, 3]
hep.histplot(h, bins)
2D Histograms
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)
More Information
Save all labels at once
hep.savelabels('test.png')
will produces 4 variation on experiment label- "" -> "test.png"
- "Preliminary" -> "test_pas.png"
- "Supplementary" -> "test_supp.png"
- "Work in Progress" -> "test_wip.png"
- Options can also be specified manually
hep.savelabels('test', labels=["FOO", "BAR"], suffixes=["foo.pdf", "bar"])
will produce- "FOO" -> "foo.pdf"
- "BAR" -> "test_bar.png"
- Other components of
<experiment>.label()
will remain unchanged.
Other styles:
hep.style.use("fira")
- use Fira Sanshep.style.use("firamath")
- use Fira Math
Styles can be chained:
- e.g.
hep.style.use(["CMS", "fira", "firamath"])
- reappearing
rcParams
get overwritten silently
Styles can be modified on the fly
- Since styles are dictionaries and they can be chained/overwritten they can be easily modified on the fly. e.g.
hep.style.use("CMS")
hep.style.use({"font.sans-serif":'Comic Sans MS'})
Styling with LaTeX
hep.style.use("CMSTex")
- Use LaTeX to produce all text labels- Requires having the full tex-live distro
- True Helvetica
- Use sansmath as the math font
- Takes longer and not always better
- In general more possibilities, but a bit more difficult to get everything working properly
Notes
Consistency & Fonts
As 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.
License
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
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.
Math Fonts
- Math fonts are a separate set from regular fonts due to the amount of special characters
- It's not trivial to make sure you get a matching math font to your regular font
- Most math-fonts are serif fonts, but this is not ideal if one wants to use sans-serif font for normal text like Helvetica or Arial
- The number of sans-serif math-fonts is very limited
- The number of open sans-serif math-fonts is extremely limited
- Basically there's two, Fira Sans and GFS Neohellenic Math, of which I like Fira Sans better
- https://tex.stackexchange.com/questions/374250/are-there-opentype-sans-math-fonts-under-development
For consistent styling Fira Sans is included as well.
Default Fira Sans
https://github.com/mozilla/Fira
Math font extension
https://github.com/firamath/firamath
What doesn't work
Context styles and fonts
with pyplot.style.context(style.ROOT):
plotting...
- This syntax would be ideal, however, it doesn't work properly for fonts and there are no plans by mpl devs to fix this behaviour https://github.com/matplotlib/matplotlib/issues/11673
For now one has to set the style globally:
Use in publications
Updating list of citations and use cases of mplhep
in publications:
- Simultaneous Jet Energy and Mass Calibrations with Neural Networks, ATLAS Collaboration, 2019
- Integration and Performance of New Technologies in the CMS Simulation, Kevin Pedro, 2020 (Fig 5,6)
- GeantV: Results from the prototype of concurrent vector particle transport simulation in HEP, Amadio et al, 2020 (Fig 25,26)
- Search for the standard model Higgs boson decaying to charm quarks, CMS Collaboration, 2019 (Fig 1)
- Search for long-lived particles decaying to eμν, LHCb Collaboration, 2020
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mplhep-0.3.23.tar.gz
.
File metadata
- Download URL: mplhep-0.3.23.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea5f7028f8d0ae7e7b0c8f63fed8ad304c07e4253022f006f4ce1eecbacb2b9d |
|
MD5 | 4f6a4836c4f2fc7b6a60f5b136151bff |
|
BLAKE2b-256 | 94e95f67aeee35f63c12ed62bf69f26ad45ecc7e9f36764096c2d4ac058d1ad0 |
File details
Details for the file mplhep-0.3.23-py3-none-any.whl
.
File metadata
- Download URL: mplhep-0.3.23-py3-none-any.whl
- Upload date:
- Size: 34.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0aaa8e9f363be335f8a8c4c75f8fa2e70ab07e76e41284dfc4470c41f67e6b4a |
|
MD5 | 9f83388a85eb128bf1cfae9e62aa0c64 |
|
BLAKE2b-256 | ec0c3061a373403cc1fda5a21ab086346d93644758a664083299ae4953acc778 |