Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e86bdf

Browse files
committed
TYP: Accept tuples in Legend handles list
Fixes #26639
1 parent fa2304c commit 6e86bdf

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def legend(self, *args, **kwargs):
280280
281281
Parameters
282282
----------
283-
handles : sequence of `.Artist`, optional
283+
handles : sequence of (`.Artist` or tuple of `.Artist`), optional
284284
A list of Artists (lines, patches) to be added to the legend.
285285
Use this together with *labels*, if you need full control on what
286286
is shown in the legend and the automatic mechanism described above
@@ -289,6 +289,9 @@ def legend(self, *args, **kwargs):
289289
The length of handles and labels should be the same in this
290290
case. If they are not, they are truncated to the smaller length.
291291
292+
If an entry contains a tuple, then the legend handler for all Artists in the
293+
tuple will be placed alongside a single label.
294+
292295
labels : list of str, optional
293296
A list of labels to show next to the artists.
294297
Use this together with *handles*, if you need full control on what

lib/matplotlib/axes/_axes.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class Axes(_AxesBase):
5757
@overload
5858
def legend(self) -> Legend: ...
5959
@overload
60-
def legend(self, handles: Sequence[Artist], labels: Sequence[str], **kwargs) -> Legend: ...
60+
def legend(self, handles: Sequence[Artist | tuple[Artist, ...]], labels: Sequence[str], **kwargs) -> Legend: ...
6161
@overload
62-
def legend(self, *, handles: Sequence[Artist], **kwargs) -> Legend: ...
62+
def legend(self, *, handles: Sequence[Artist | tuple[Artist, ...]], **kwargs) -> Legend: ...
6363
@overload
6464
def legend(self, labels: Sequence[str], **kwargs) -> Legend: ...
6565
@overload

lib/matplotlib/legend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def __init__(
401401
parent : `~matplotlib.axes.Axes` or `.Figure`
402402
The artist that contains the legend.
403403
404-
handles : list of `.Artist`
404+
handles : list of (`.Artist` or tuple of `.Artist`)
405405
A list of Artists (lines, patches) to be added to the legend.
406406
407407
labels : list of str
@@ -1322,7 +1322,7 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
13221322
13231323
Returns
13241324
-------
1325-
handles : list of `.Artist`
1325+
handles : list of (`.Artist` or tuple of `.Artist`)
13261326
The legend handles.
13271327
labels : list of str
13281328
The legend labels.

lib/matplotlib/legend.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Legend(Artist):
5252
def __init__(
5353
self,
5454
parent: Axes | Figure,
55-
handles: Iterable[Artist],
55+
handles: Iterable[Artist | tuple[Artist, ...]],
5656
labels: Iterable[str],
5757
*,
5858
loc: str | tuple[float, float] | int | None = ...,

0 commit comments

Comments
 (0)