alphapepttools.pl.add_legend_to_axes

alphapepttools.pl.add_legend_to_axes#

alphapepttools.pl.add_legend_to_axes(ax, levels=None, legend='auto', palette=None, **legend_kwargs)#

Add a legend to an axis object.

Handle legend creation in three ways: 1.: ‘levels’ is a dictionary of levels and colors, in which case these levels and colors are used directly. 2.: ‘levels’ is a list of levels, in which case a color palette is used to assign colors to levels. A custom palette can be provided, otherwise a default palette is used. 3.: ‘legend’ is a matplotlib legend object, which overrides all other options and is added directly to the axes. This defaults to ‘auto’, which directs to the first two cases.

Parameters:
  • ax (plt.Axes) – Matplotlib axes object to add the legend to.

  • levels (list[str] | dict[str, str | tuple] | None) – List of levels to use for the legend. Duplicates are removed. Colors from the palette are assigned to unique values from this list, but no particular color-binding is enforced. If this is a dictionary, the legend contains exactly the labels (keys) and colors (values) provided.

  • legend (str | mpl.legend.Legend | None) – Legend to add to the plot. If “auto”, a legend is created based on levels. If a Legend object, it is added directly to the axes. By default “auto”.

  • palette (list[str | tuple] | None) – List of colors to use for the legend. If None, a default palette will be used. By default None. Only relevant when levels is a list, i.e. when matching of values to colors happens automatically.

  • legend_kwargs (dict, optional) – Additional keyword arguments for the legend, by default {}. This can include ‘fontsize’, ‘title’, etc. These kwargs are not enforced if a matplotlib legend object is passed as the legend parameter.

Return type:

None