Skip to main content

Matplotlib styles for HEP

Project description

DOI Scikit-HEP

Docs PyPI 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

Styling

import mplhep as hep
hep.set_style(hep.style.ROOT) # For now ROOT defaults to CMS
# Or choose one of the experiment styles
hep.set_style(hep.style.CMS)
# or
hep.set_style("ATLAS") # string aliases work too
# or
hep.set_style("LHCb2")  # or "LHCb1"
# or
hep.set_style("ALICE")

The style can be set directly from the matplotlib API as well

import matplotlib.pyplot as plt
import mplhep as hep
plt.style.use(hep.style.ROOT)

Experiment specific style are also available. If the default styles are not what you need, I'd be happy to merge in new styles or modify the current ones.

Default experiment labels are also available.

# Overall - both left and right annotation
hep.<experiment>.label()
# Just experiment label and <text> such as 'Preliminary' or 'Simulation'
hep.<experiment>.text(<text>)

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

Other styles:

  • hep.set_style("fira") - use Fira Sans
  • hep.set_style("firamath") - use Fira Math

Styles can be chained:

  • e.g. hep.set_style(["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.set_style("CMS")
hep.set_style({"font.sans-serif":'Comic Sans MS'})

Styling with LaTeX

  • hep.set_style("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.2.14.tar.gz (5.4 MB view details)

Uploaded Source

Built Distribution

mplhep-0.2.14-py3-none-any.whl (5.4 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mplhep-0.2.14.tar.gz
  • Upload date:
  • Size: 5.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for mplhep-0.2.14.tar.gz
Algorithm Hash digest
SHA256 3cdf7502f7a243ca6a0dffba8e063764888ada415f7853fb7b464270841e7754
MD5 a23dafce8c0abdfbb0400c551ff5d46d
BLAKE2b-256 572604651b536d9abfa40c46affbc57148c035a9e15d7ccfbdab8a8eeab1fd91

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mplhep-0.2.14-py3-none-any.whl
  • Upload date:
  • Size: 5.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for mplhep-0.2.14-py3-none-any.whl
Algorithm Hash digest
SHA256 151aff6f9e99b7e4f67722dc0c8c20a46d7312bab21b212bbaa206166621b1f0
MD5 939be482ffd80787778c0b47a58f0e43
BLAKE2b-256 be456d12b0f3b2ae9a5116a90e22bf04ab5cb27e539b2995645229fd31a8f576

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