hyperfine.utilities.split

hyperfine.utilities.split(handles_labels: Sequence[T], plot_type: str) Sequence[T][source]

Split legend handles/labels based on plot type.

Split the sequence of legend handles/labels based on the underlying plot type, which is useful for creating multiple legends on a single figure.

Adapted from: https://stackoverflow.com/a/76485654

Parameters:
  • handles_labels – Sequence of plot handles/labels.

  • plot_type – Type of plot to “pluck” from the sequence of plot handles/labels.

Returns:

The subset of the sequence of plot handles/labels corresponding to the desired plot type.

Example

import numpy as np
import matplotlib.pyplot as plt
from hyperfine.utilities import split

x = np.linspace(0, 10, 10)
y = 2 * x

fig, ax = plt.subplots(1, 1)
ax.plot(x, y, "-", zorder=1, label="plot")
ax.axvspan(5, 9, color="lightgrey", zorder=0, label="axvspan")

handles_labels = ax.get_legend_handles_labels()

l_1 = ax.legend(*split(handles_labels, "axvspan"), loc="upper left")
ax.add_artist(l_1)
l_2 = ax.legend(*split(handles_labels, "plot"), loc="lower right")

plt.show()

(Source code, png, hires.png, pdf)

../_images/hyperfine-utilities-split-1.png