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
)
| 984 | # Copied from matplotlib, tweaked so func can return strings. |
| 985 | # https://github.com/matplotlib/matplotlib/issues/19555 |
| 986 | def 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 |
no test coverage detected
searching dependent graphs…