|
27 | 27 | def legend_artist(self, legend, orig_handle, fontsize, handlebox) |
28 | 28 | """ |
29 | 29 |
|
| 30 | +from collections.abc import Sequence |
30 | 31 | from itertools import cycle |
31 | 32 |
|
32 | 33 | import numpy as np |
33 | 34 |
|
34 | | -from matplotlib import cbook |
| 35 | +from matplotlib import _api, cbook |
35 | 36 | from matplotlib.lines import Line2D |
36 | 37 | from matplotlib.patches import Rectangle |
37 | 38 | import matplotlib.collections as mcoll |
@@ -119,6 +120,9 @@ def legend_artist(self, legend, orig_handle, |
119 | 120 | xdescent, ydescent, width, height, |
120 | 121 | fontsize, handlebox.get_transform()) |
121 | 122 |
|
| 123 | + if isinstance(artists, _Line2DHandleList): |
| 124 | + artists = [artists[0]] |
| 125 | + |
122 | 126 | # create_artists will return a list of artists. |
123 | 127 | for a in artists: |
124 | 128 | handlebox.add_artist(a) |
@@ -254,6 +258,24 @@ def create_artists(self, legend, orig_handle, |
254 | 258 | return [legline, legline_marker] |
255 | 259 |
|
256 | 260 |
|
| 261 | +class _Line2DHandleList(Sequence): |
| 262 | + def __init__(self, legline): |
| 263 | + self._legline = legline |
| 264 | + |
| 265 | + def __len__(self): |
| 266 | + return 2 |
| 267 | + |
| 268 | + def __getitem__(self, index): |
| 269 | + if index != 0: |
| 270 | + # Make HandlerLine2D return [self._legline] directly after |
| 271 | + # deprecation elapses. |
| 272 | + _api.warn_deprecated( |
| 273 | + "3.5", message="Access to the second element returned by " |
| 274 | + "HandlerLine2D is deprecated since %(since)s; it will be " |
| 275 | + "removed %(removal)s.") |
| 276 | + return [self._legline, self._legline][index] |
| 277 | + |
| 278 | + |
257 | 279 | class HandlerLine2D(HandlerNpoints): |
258 | 280 | """ |
259 | 281 | Handler for `.Line2D` instances. |
@@ -304,7 +326,7 @@ def create_artists(self, legend, orig_handle, |
304 | 326 |
|
305 | 327 | legline.set_transform(trans) |
306 | 328 |
|
307 | | - return [legline] |
| 329 | + return _Line2DHandleList(legline) |
308 | 330 |
|
309 | 331 |
|
310 | 332 | class HandlerPatch(HandlerBase): |
@@ -765,6 +787,8 @@ def create_artists(self, legend, orig_handle, |
765 | 787 | _a_list = handler.create_artists( |
766 | 788 | legend, handle1, |
767 | 789 | next(xds_cycle), ydescent, width, height, fontsize, trans) |
| 790 | + if isinstance(_a_list, _Line2DHandleList): |
| 791 | + _a_list = [_a_list[0]] |
768 | 792 | a_list.extend(_a_list) |
769 | 793 |
|
770 | 794 | return a_list |
|
0 commit comments