Skip to main content

Matplotlib styles for HEP

Project description

DOI Scikit-HEP

Docs PyPI version Conda-forge version Supported Python versions

Build Status GitHub Actions Status: CI GitHub Actions Status: Publish pre-commit.ci status

PyPI download week

Binder

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 Sans
  • hep.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. Therfore 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 respecively.

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

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...

For now one has to set the style globally:

Use in publications

Updating list of citations and use cases of mplhep in publications:

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

mplhep-0.3.11.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

mplhep-0.3.11-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file mplhep-0.3.11.tar.gz.

File metadata

  • Download URL: mplhep-0.3.11.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for mplhep-0.3.11.tar.gz
Algorithm Hash digest
SHA256 676376f75ed11e558d9c5c9a2ab4adc77d1c961f4be59e56b687ea433d61a2eb
MD5 b0b045e31d75fa964b0bde3dbb035bbe
BLAKE2b-256 16fb33174a3e8db435103ea1a346dbb793c2317738bf994af64bdfbe6f981e90

See more details on using hashes here.

File details

Details for the file mplhep-0.3.11-py3-none-any.whl.

File metadata

  • Download URL: mplhep-0.3.11-py3-none-any.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for mplhep-0.3.11-py3-none-any.whl
Algorithm Hash digest
SHA256 88f7b1d6cefae9054d14eee1c4e2aef88f494b64f5de6d1574c2dd512b3081d2
MD5 55e0c6021a2b3a2fc49e90626a99f591
BLAKE2b-256 afe8d142cc08180ad7327e036dd5faf96261349a9ab9090b4847c29e2509820f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page