matplotlib 3.5.1

ParametersReturns
legend_elements(self, prop='colors', num='auto', fmt=None, func=<function PathCollection.<lambda> at 0x0000000>, **kwargs)

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.scatter([1, 2, 3],  [4, 5, 6],  c=[7, 2, 3])
plt.legend(*scatter.legend_elements())

creates three legend elements, one for each color with the numerical values passed to c as the labels.

Also see the automatedlegendcreation example.

Parameters

prop : {"colors", "sizes"}, default: "colors"

If "colors", the legend handles will show the different colors of the collection. If "sizes", the legend will show the different sizes. To set both, use kwargs to directly edit the .Line2D properties.

num : int, None, "auto" (default), array-like, or `~.ticker.Locator`

Target number of elements to create. If None, use all unique elements of the mappable array. If an integer, target to use num elements in the normed range. If "auto", try to determine which option better suits the nature of the data. The number of created elements may slightly deviate from num due to a ~.ticker.Locator being used to find useful locations. If a list or array, use exactly those elements for the legend. Finally, a ~.ticker.Locator can be provided.

fmt : str, `~matplotlib.ticker.Formatter`, or None (default)

The format or formatter to use for the labels. If a string must be a valid input for a .StrMethodFormatter . If None (the default), use a .ScalarFormatter .

func : function, default: ``lambda x: x``

Function to calculate the labels. Often the size (or color) argument to ~.Axes.scatter will have been pre-processed by the user using a function s = f(x) to make the markers visible; e.g. size = np.log10(x) . Providing the inverse of this function here allows that pre-processing to be inverted, so that the legend labels have the correct values; e.g. func = lambda x: 10**x .

**kwargs :

Allowed keyword arguments are color and size. E.g. it may be useful to set the color of the markers if prop="sizes" is used; similarly to set the size of the markers if prop="colors" is used. Any further parameters are passed onto the .Line2D instance. This may be useful to e.g. specify a different markeredgecolor or alpha for the legend handles.

Returns

handles : list of `.Line2D`

Visual representation of each element of the legend.

labels : list of str

The string labels for elements of the legend.

Create legend handles and labels for a PathCollection.

Examples

See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /matplotlib/collections.py#1025
type: <class 'function'>
Commit: