"""Plotting utilities and premade plot configurations"""
[docs]
def hist_overflow(val, val_max, **kwds):
"""Make a histogram with an overflow bar above val_max"""
from matplotlib import pyplot as plt
overflow = len(val[val >= val_max])
plt.hist(val[val < val_max], **kwds)
color = kwds.get("color")
if overflow > 0:
rect = plt.bar(val_max + 0.05, overflow, 0.5, color=color)[0]
plt.text(rect.get_x(), 1.10 * rect.get_height(), f"{val_max}+")
[docs]
def add_style_opt_to_parser(parser, default=None):
"""
Adds an option to set the matplotlib style to a parser.
Parameters
----------
parser : argparse.ArgumentParser
The parser to add the option to.
default : str, optional
The default style to use. Default, None, will result in the default
matplotlib style to be used.
"""
from matplotlib import pyplot
parser.add_argument(
"--mpl-style",
default=default,
choices=["default", *pyplot.style.available, "xkcd"],
help="Set the matplotlib style to use.",
)
[docs]
def set_style_from_cli(opts):
"""
Uses the mpl-style option to set the style for plots.
Note: This will change the global rcParams.
"""
from matplotlib import pyplot
if opts.mpl_style == "xkcd":
# this is treated differently for some reason
pyplot.xkcd()
elif opts.mpl_style is not None:
pyplot.style.use(opts.mpl_style)