sorted_legend_handles_labels

typhon.plots.sorted_legend_handles_labels(ax=None, key=None, reverse=True)[source]

Sort legend labels and handles.

Returns legend handles and labels in descending order of y data peak values.

Parameters:
  • ax – Matplotlib axis.

  • key (Callable) – Function that takes Line2D object. See example below.

  • reverse (bool) – Default: True

Returns:

Sorted legend handles and labels

Return type:

Tuple(handles, labels)

Example

>>> # Sort by maximum y value
>>> ax.legend(*sorted_legend_handles_labels())
>>> # Sort by minimum y value
>>> ax.legend(*sorted_legend_handles_labels(
...           key=lambda line: np.min(line.get_ydata())))