MCPcopy
hub / github.com/pydata/xarray / legend_elements

Function legend_elements

xarray/plot/utils.py:986–1177  ·  view source on GitHub ↗

Create legend handles and labels for a PathCollection. Each legend handle is a `.Line2D` representing the Path that was drawn, and each label is a string what each Path represents. This is useful for obtaining a legend for a `~.Axes.scatter` plot; e.g.:: scatter = plt

(
    self, prop="colors", num="auto", fmt=None, func=lambda x: x, **kwargs
)

Source from the content-addressed store, hash-verified

984# Copied from matplotlib, tweaked so func can return strings.
985# https://github.com/matplotlib/matplotlib/issues/19555
986def legend_elements(
987 self, prop="colors", num="auto", fmt=None, func=lambda x: x, **kwargs
988):
989 """
990 Create legend handles and labels for a PathCollection.
991
992 Each legend handle is a `.Line2D` representing the Path that was drawn,
993 and each label is a string what each Path represents.
994
995 This is useful for obtaining a legend for a `~.Axes.scatter` plot;
996 e.g.::
997
998 scatter = plt.scatter([1, 2, 3], [4, 5, 6], c=[7, 2, 3])
999 plt.legend(*scatter.legend_elements())
1000
1001 creates three legend elements, one for each color with the numerical
1002 values passed to *c* as the labels.
1003
1004 Also see the :ref:`automatedlegendcreation` example.
1005
1006
1007 Parameters
1008 ----------
1009 prop : {"colors", "sizes"}, default: "colors"
1010 If "colors", the legend handles will show the different colors of
1011 the collection. If "sizes", the legend will show the different
1012 sizes. To set both, use *kwargs* to directly edit the `.Line2D`
1013 properties.
1014 num : int, None, "auto" (default), array-like, or `~.ticker.Locator`
1015 Target number of elements to create.
1016 If None, use all unique elements of the mappable array. If an
1017 integer, target to use *num* elements in the normed range.
1018 If *"auto"*, try to determine which option better suits the nature
1019 of the data.
1020 The number of created elements may slightly deviate from *num* due
1021 to a `~.ticker.Locator` being used to find useful locations.
1022 If a list or array, use exactly those elements for the legend.
1023 Finally, a `~.ticker.Locator` can be provided.
1024 fmt : str, `~matplotlib.ticker.Formatter`, or None (default)
1025 The format or formatter to use for the labels. If a string must be
1026 a valid input for a `~.StrMethodFormatter`. If None (the default),
1027 use a `~.ScalarFormatter`.
1028 func : function, default: ``lambda x: x``
1029 Function to calculate the labels. Often the size (or color)
1030 argument to `~.Axes.scatter` will have been pre-processed by the
1031 user using a function ``s = f(x)`` to make the markers visible;
1032 e.g. ``size = np.log10(x)``. Providing the inverse of this
1033 function here allows that pre-processing to be inverted, so that
1034 the legend labels have the correct values; e.g. ``func = lambda
1035 x: 10**x``.
1036 **kwargs
1037 Allowed keyword arguments are *color* and *size*. E.g. it may be
1038 useful to set the color of the markers if *prop="sizes"* is used;
1039 similarly to set the size of the markers if *prop="colors"* is
1040 used. Any further parameters are passed onto the `.Line2D`
1041 instance. This may be useful to e.g. specify a different
1042 *markeredgecolor* or *alpha* for the legend handles.
1043

Callers 1

_add_legendFunction · 0.85

Calls 11

typeFunction · 0.85
_get_color_and_sizeFunction · 0.85
linspaceMethod · 0.80
funcFunction · 0.50
get_arrayMethod · 0.45
minMethod · 0.45
maxMethod · 0.45
argsortMethod · 0.45
interpMethod · 0.45
astypeMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…