MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / legend_elements

Method legend_elements

lib/matplotlib/collections.py:1154–1297  ·  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 that represents the Path. This is useful for obtaining a legend for a `~.Axes.scatter` plot; e.g.::

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

Source from the content-addressed store, hash-verified

1152 return self._paths
1153
1154 def legend_elements(self, prop="colors", num="auto",
1155 fmt=None, func=lambda x: x, **kwargs):
1156 """
1157 Create legend handles and labels for a PathCollection.
1158
1159 Each legend handle is a `.Line2D` representing the Path that was drawn,
1160 and each label is a string that represents the Path.
1161
1162 This is useful for obtaining a legend for a `~.Axes.scatter` plot;
1163 e.g.::
1164
1165 scatter = plt.scatter([1, 2, 3], [4, 5, 6], c=[7, 2, 3], num=None)
1166 plt.legend(*scatter.legend_elements())
1167
1168 creates three legend elements, one for each color with the numerical
1169 values passed to *c* as the labels.
1170
1171 Also see the :ref:`automatedlegendcreation` example.
1172
1173 Parameters
1174 ----------
1175 prop : {"colors", "sizes"}, default: "colors"
1176 If "colors", the legend handles will show the different colors of
1177 the collection. If "sizes", the legend will show the different
1178 sizes. To set both, use *kwargs* to directly edit the `.Line2D`
1179 properties.
1180 num : int, None, "auto" (default), array-like, or `~.ticker.Locator`
1181 Target number of elements to create.
1182 If None, use all unique elements of the mappable array. If an
1183 integer, target to use *num* elements in the normed range.
1184 If *"auto"*, try to determine which option better suits the nature
1185 of the data.
1186 The number of created elements may slightly deviate from *num* due
1187 to a `~.ticker.Locator` being used to find useful locations.
1188 If a list or array, use exactly those elements for the legend.
1189 Finally, a `~.ticker.Locator` can be provided.
1190 fmt : str, `~matplotlib.ticker.Formatter`, or None (default)
1191 The format or formatter to use for the labels. If a string must be
1192 a valid input for a `.StrMethodFormatter`. If None (the default),
1193 use a `.ScalarFormatter`.
1194 func : function, default: ``lambda x: x``
1195 Function to calculate the labels. Often the size (or color)
1196 argument to `~.Axes.scatter` will have been pre-processed by the
1197 user using a function ``s = f(x)`` to make the markers visible;
1198 e.g. ``size = np.log10(x)``. Providing the inverse of this
1199 function here allows that pre-processing to be inverted, so that
1200 the legend labels have the correct values; e.g. ``func = lambda
1201 x: 10**x``.
1202 **kwargs
1203 Allowed keyword arguments are *color* and *size*. E.g. it may be
1204 useful to set the color of the markers if *prop="sizes"* is used;
1205 similarly to set the size of the markers if *prop="colors"* is
1206 used. Any further parameters are passed onto the `.Line2D`
1207 instance. This may be useful to e.g. specify a different
1208 *markeredgecolor* or *alpha* for the legend handles.
1209
1210 Returns
1211 -------

Callers

nothing calls this directly

Calls 15

tick_valuesMethod · 0.95
get_pathsMethod · 0.95
fmtFunction · 0.85
get_arrayMethod · 0.80
create_dummy_axisMethod · 0.80
minMethod · 0.80
maxMethod · 0.80
get_linewidthsMethod · 0.80
sqrtMethod · 0.80
iscloseMethod · 0.80
funcFunction · 0.50
popMethod · 0.45

Tested by

no test coverage detected