|
| 1 | +# Utilities |
| 2 | + |
| 3 | +This section covers advanced mplhep features and utilities. |
| 4 | + |
| 5 | +??? tip "Prerequisites" |
| 6 | + Throughout this guide the following codeblock is assumed. |
| 7 | + ```python |
| 8 | + import matplotlib.pyplot as plt |
| 9 | + import numpy as np |
| 10 | + import hist |
| 11 | + np.random.seed(42) |
| 12 | + import mplhep as mh |
| 13 | + # mh.style.use('<as appropriate>') |
| 14 | + ``` |
| 15 | + |
| 16 | +## Text placement |
| 17 | + |
| 18 | +### Add text |
| 19 | + |
| 20 | +[mh.add_text][mplhep.add_text] will add text to a specified location on the axis. It has flexible positioning options, that are accessible via the `loc` argument, or the `x` and `y` arguments. |
| 21 | + |
| 22 | +{{TABS_START}} |
| 23 | +{{TAB_HEADER}} |
| 24 | + |
| 25 | + === "loc argument" |
| 26 | + |
| 27 | + ```python |
| 28 | + # mkdocs: render |
| 29 | + # mkdocs: align=left |
| 30 | + import matplotlib.pyplot as plt # mkdocs: hide |
| 31 | + import mplhep as mh # mkdocs: hide |
| 32 | + {{STYLE_USE_CODE}} # mkdocs: hide |
| 33 | + fig, ax = plt.subplots() |
| 34 | + |
| 35 | + locs = [ |
| 36 | + "upper left", # or "top left" |
| 37 | + "upper right", # or "top right" |
| 38 | + "lower left", # or "bottom left" |
| 39 | + "lower right", # or "bottom right" |
| 40 | + "over left", |
| 41 | + "over right", |
| 42 | + "under left", |
| 43 | + "under right", |
| 44 | + ] |
| 45 | + |
| 46 | + for loc in locs: |
| 47 | + mh.add_text( |
| 48 | + f'Text with\nloc="{loc}"', |
| 49 | + loc=loc, |
| 50 | + ax=ax, |
| 51 | + ) |
| 52 | + ``` |
| 53 | + |
| 54 | + === "x and y arguments" |
| 55 | + |
| 56 | + ```python |
| 57 | + # mkdocs: render |
| 58 | + # mkdocs: align=left |
| 59 | + import matplotlib.pyplot as plt # mkdocs: hide |
| 60 | + import mplhep as mh # mkdocs: hide |
| 61 | + {{STYLE_USE_CODE}} # mkdocs: hide |
| 62 | + fig, ax = plt.subplots() |
| 63 | + |
| 64 | + positions = [ |
| 65 | + ("right_in", "top_in"), |
| 66 | + ("left_in", "top_in"), |
| 67 | + ("left_in", "bottom_in"), |
| 68 | + ("right_in", "bottom_in"), |
| 69 | + ("right", "top_out"), |
| 70 | + ("left", "top_out"), |
| 71 | + ("right_out", "top_in"), |
| 72 | + ("right_out", "bottom_in"), |
| 73 | + ("right", "bottom_out"), |
| 74 | + ("left", "bottom_out"), |
| 75 | + ] |
| 76 | + |
| 77 | + for x, y in positions: |
| 78 | + mh.add_text( |
| 79 | + f'Text with\nx="{x}"\ny="{y}"', |
| 80 | + x=x, |
| 81 | + y=y, |
| 82 | + ax=ax, |
| 83 | + ) |
| 84 | + ``` |
| 85 | + |
| 86 | +{{TABS_END}} |
| 87 | + |
| 88 | +### Append text |
| 89 | + |
| 90 | +[mh.append_text][mplhep.append_text] appends additional text relative to an existing text object created with [mh.add_text][mplhep.add_text]. The new text can be positioned above, below, left, or right of the existing text. |
| 91 | + |
| 92 | +```python |
| 93 | +# Add text at specific location |
| 94 | +txt = mh.add_text('Custom Text', loc='upper right') |
| 95 | + |
| 96 | +# Append additional text |
| 97 | +mh.append_text('Additional info', txt, loc='below') |
| 98 | +``` |
| 99 | + |
| 100 | +## Subplot creation |
| 101 | + |
| 102 | +[mh.subplots][mplhep.subplots] is a wrapper around `plt.subplots` to create a figure with multiple subplots. It conveniently adjusts the figure size and spacing between subplots if multiple rows are requested. |
| 103 | + |
| 104 | +```python |
| 105 | +fig, axes = mh.subplots(nrows=6) |
| 106 | +# Will scale the figure, and add spacing between rows and remove the xlabels on inner plots. |
| 107 | +``` |
| 108 | + |
| 109 | +## Save variations |
| 110 | + |
| 111 | +[mh.savelabels][mplhep.savelabels] automatically generates multiple versions of a plot with different experiment label text variations, useful for creating preliminary and final versions of plots. |
| 112 | + |
| 113 | +```python |
| 114 | +mh.savelabels('test.png') |
| 115 | +# Produces: test.png, test_pas.png, test_supp.png, test_wip.png, with no label, 'Preliminary', 'Supplementary', and 'Work in Progress' labels respectively. |
| 116 | + |
| 117 | +mh.savelabels('test', labels=[("FOO", "foo.pdf"), ("BAR", "bar")]) |
| 118 | +# Produces: foo.pdf, test_bar.png |
| 119 | +``` |
| 120 | + |
| 121 | +## Fit y-label |
| 122 | + |
| 123 | +[mh.set_fitting_ylabel_fontsize][mplhep.set_fitting_ylabel_fontsize] adjusts the y-axis label font size to fit within the figure when there are long y-axis labels. |
| 124 | + |
| 125 | +```python |
| 126 | +mh.set_fitting_ylabel_fontsize(ax) |
| 127 | +``` |
| 128 | + |
| 129 | +## mpl_magic |
| 130 | + |
| 131 | +The function [mh.mpl_magic][mplhep.mpl_magic] applies several common mplhep utilities to a given axis: |
| 132 | + |
| 133 | +- [mh.set_ylow][mplhep.set_ylow]: Sets a minimum y-axis limit based on data. |
| 134 | +- [mh.yscale_legend][mplhep.yscale_legend]: Rescales the y-axis to fit the legend. |
| 135 | +- [mh.yscale_anchored_text][mplhep.yscale_anchored_text]: Rescales the y-axis to fit anchored text. |
| 136 | + |
| 137 | +```python |
| 138 | +mh.mpl_magic(ax) |
| 139 | +``` |
| 140 | + |
| 141 | +You can also call these functions individually as needed. |
| 142 | + |
| 143 | +## Axes manipulation |
| 144 | + |
| 145 | +### Add colorbar axis |
| 146 | + |
| 147 | +[mh.make_square_add_cbar][mplhep.make_square_add_cbar] creates a square axis and adds a colorbar axis to the right, useful for 2D histograms. |
| 148 | + |
| 149 | +```python |
| 150 | +ax_colorbar = mh.make_square_add_cbar(ax) |
| 151 | +# ax is now square, and ax_colorbar is the colorbar axis. |
| 152 | +``` |
| 153 | + |
| 154 | +### Add axis |
| 155 | + |
| 156 | +[mh.append_axes][mplhep.append_axes] appends a new axis to an existing axis in a specified direction (top, bottom, left, right). |
| 157 | + |
| 158 | +```python |
| 159 | +ax_new = mh.append_axes(ax, position='top', size=2, pad=0.3) |
| 160 | +# Adds a new axis above ax with height 2 inches and 0.3 inch padding. |
| 161 | +``` |
| 162 | + |
| 163 | +## plt.hist wrapper |
| 164 | + |
| 165 | +[mh.hist][mplhep.hist] is a drop-in replacement for [mh.histplot][mplhep.histplot] which runs the `np.histogram` function before plotting. It provides a convenient way to create a histogram of raw data values and benefits from the extended features of [mh.histplot][mplhep.histplot], such as automatic error bar calculation, bin-width normalization and HEP-style plotting options. |
| 166 | + |
| 167 | +!!! warning |
| 168 | + [mh.hist][mplhep.hist] does not return a histogram object compatible with the Unified Histogram Interface (UHI), thus cannot be used directly with other mplhep histogram plotting functions. |
| 169 | + |
| 170 | +```python |
| 171 | +data = np.random.normal(100, 15, 1000) |
| 172 | +mh.hist(data, bins=50, range=(50, 150)) |
| 173 | + |
| 174 | +# Multiple datasets |
| 175 | +data1 = np.random.normal(100, 15, 1000) |
| 176 | +data2 = np.random.normal(120, 15, 1000) |
| 177 | +mh.hist([data1, data2], bins=50, label=['Dataset 1', 'Dataset 2']) |
| 178 | +``` |
0 commit comments