From cd057a986ba188de844976dc112c87b959341502 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 8 Apr 2021 02:34:38 -0400 Subject: [PATCH] Implement ArtistList.__[r]add__. Fixes #19890. --- lib/matplotlib/axes/_base.py | 10 ++++++++++ lib/matplotlib/tests/test_axes.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 28eaaa827d47..8dfce27bca1f 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1363,6 +1363,16 @@ def __getitem__(self, key): for artist in self._axes._children if self._type_check(artist)][key] + def __add__(self, other): + if isinstance(other, (list, _AxesBase.ArtistList)): + return [*self, *other] + return NotImplemented + + def __radd__(self, other): + if isinstance(other, list): + return other + list(self) + return NotImplemented + def insert(self, index, item): _api.warn_deprecated( '3.5', diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index a44cecbe162f..2ff65bcad240 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7104,3 +7104,7 @@ def test_artist_sublists(): match='modification of the Axes.lines property'): ax.lines[1:1] = lines[1:-2] assert list(ax.lines) == lines + + # Adding to other lists should produce a regular list. + assert ax.lines + [1, 2, 3] == [*lines, 1, 2, 3] + assert [1, 2, 3] + ax.lines == [1, 2, 3, *lines]