Skip to content

Commit 278de8f

Browse files
committed
Move Axes.images into hidden children attribute.
The images can still be accessed via a read-only property, but now are combined with lines, patches, tables and texts for sorting and drawing purposes.
1 parent 71f5775 commit 278de8f

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,6 @@ def cla(self):
12331233
self._gridOn = mpl.rcParams['axes.grid']
12341234
self._children = []
12351235
self.artists = []
1236-
self.images = []
12371236
self._mouseover_set = _OrderedSet()
12381237
self.child_axes = []
12391238
self._current_image = None # strictly for pyplot via _sci, _gci
@@ -1307,6 +1306,11 @@ def cla(self):
13071306

13081307
self.stale = True
13091308

1309+
@property
1310+
def images(self):
1311+
return tuple(a for a in self._children
1312+
if isinstance(a, mimage.AxesImage))
1313+
13101314
@property
13111315
def lines(self):
13121316
return tuple(a for a in self._children if isinstance(a, mlines.Line2D))
@@ -2097,13 +2101,13 @@ def add_collection(self, collection, autolim=True):
20972101

20982102
def add_image(self, image):
20992103
"""
2100-
Add an `~.AxesImage` to the axes' images; return the image.
2104+
Add an `~.AxesImage` to the Axes; return the image.
21012105
"""
21022106
self._set_artist_props(image)
21032107
if not image.get_label():
2104-
image.set_label('_image%d' % len(self.images))
2105-
self.images.append(image)
2106-
image._remove_method = self.images.remove
2108+
image.set_label(f'_child{len(self._children)}')
2109+
self._children.append(image)
2110+
image._remove_method = self._children.remove
21072111
self.stale = True
21082112
return image
21092113

@@ -2911,8 +2915,9 @@ def draw(self, renderer=None, inframe=False):
29112915
artists.remove(self._right_title)
29122916

29132917
if not self.figure.canvas.is_saving():
2914-
artists = [a for a in artists
2915-
if not a.get_animated() or a in self.images]
2918+
artists = [
2919+
a for a in artists
2920+
if not a.get_animated() or isinstance(a, mimage.AxesImage)]
29162921
artists = sorted(artists, key=attrgetter('zorder'))
29172922

29182923
# rasterize artists with negative zorder
@@ -4347,7 +4352,6 @@ def get_children(self):
43474352
*self.spines.values(),
43484353
*self._get_axis_list(),
43494354
self.title, self._left_title, self._right_title,
4350-
*self.images,
43514355
*self.child_axes,
43524356
*([self.legend_] if self.legend_ is not None else []),
43534357
self.patch,

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from matplotlib import _api
44
import matplotlib.artist as martist
5+
import matplotlib.image as mimage
56
import matplotlib.transforms as mtransforms
67
from matplotlib.axes import subplot_class_factory
78
from matplotlib.transforms import Bbox
@@ -24,9 +25,18 @@ def cla(self):
2425
self._get_lines = self._parent_axes._get_lines
2526

2627
def get_images_artists(self):
27-
artists = {a for a in self.get_children() if a.get_visible()}
28-
images = {a for a in self.images if a.get_visible()}
29-
return list(images), list(artists - images)
28+
artists = []
29+
images = []
30+
31+
for a in self.get_children():
32+
if not a.get_visible():
33+
continue
34+
if isinstance(a, mimage.AxesImage):
35+
images.append(a)
36+
else:
37+
artists.append(a)
38+
39+
return images, artists
3040

3141
def pick(self, mouseevent):
3242
# This most likely goes to Artist.pick (depending on axes_class given
@@ -204,7 +214,7 @@ def _get_legend_handles(self, legend_handler_map=None):
204214
def draw(self, renderer):
205215

206216
orig_artists = list(self.artists)
207-
orig_images = list(self.images)
217+
orig_children_len = len(self._children)
208218

209219
if hasattr(self, "get_axes_locator"):
210220
locator = self.get_axes_locator()
@@ -222,12 +232,12 @@ def draw(self, renderer):
222232
for ax in self.parasites:
223233
ax.apply_aspect(rect)
224234
images, artists = ax.get_images_artists()
225-
self.images.extend(images)
235+
self._children.extend(images)
226236
self.artists.extend(artists)
227237

228238
super().draw(renderer)
229239
self.artists = orig_artists
230-
self.images = orig_images
240+
self._children = self._children[:orig_children_len]
231241

232242
def cla(self):
233243
for ax in self.parasites:

0 commit comments

Comments
 (0)