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

Skip to content

Commit 49df465

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 5f0ca79 commit 49df465

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,6 @@ def cla(self):
11061106
self._gridOn = mpl.rcParams['axes.grid']
11071107
self._children = []
11081108
self.artists = []
1109-
self.images = []
11101109
self._mouseover_set = _OrderedSet()
11111110
self.child_axes = []
11121111
self._current_image = None # strictly for pyplot via _sci, _gci
@@ -1179,6 +1178,11 @@ def cla(self):
11791178

11801179
self.stale = True
11811180

1181+
@property
1182+
def images(self):
1183+
return tuple(a for a in self._children
1184+
if isinstance(a, mimage.AxesImage))
1185+
11821186
@property
11831187
def lines(self):
11841188
return tuple(a for a in self._children if isinstance(a, mlines.Line2D))
@@ -1967,13 +1971,13 @@ def add_collection(self, collection, autolim=True):
19671971

19681972
def add_image(self, image):
19691973
"""
1970-
Add an `~.AxesImage` to the axes' images; return the image.
1974+
Add an `~.AxesImage` to the Axes; return the image.
19711975
"""
19721976
self._set_artist_props(image)
19731977
if not image.get_label():
1974-
image.set_label('_image%d' % len(self.images))
1975-
self.images.append(image)
1976-
image._remove_method = self.images.remove
1978+
image.set_label(f'_image{len(self._children)}')
1979+
self._children.append(image)
1980+
image._remove_method = self._children.remove
19771981
self.stale = True
19781982
return image
19791983

@@ -2750,8 +2754,9 @@ def draw(self, renderer=None, inframe=False):
27502754
artists.remove(self._right_title)
27512755

27522756
if not self.figure.canvas.is_saving():
2753-
artists = [a for a in artists
2754-
if not a.get_animated() or a in self.images]
2757+
artists = [
2758+
a for a in artists
2759+
if not a.get_animated() or isinstance(a, mimage.AxesImage)]
27552760
artists = sorted(artists, key=attrgetter('zorder'))
27562761

27572762
# rasterize artists with negative zorder
@@ -4078,7 +4083,6 @@ def get_children(self):
40784083
*self.spines.values(),
40794084
*self._get_axis_list(),
40804085
self.title, self._left_title, self._right_title,
4081-
*self.images,
40824086
*self.child_axes,
40834087
*([self.legend_] if self.legend_ is not None else []),
40844088
self.patch,

lib/mpl_toolkits/axes_grid1/parasite_axes.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22

3-
from matplotlib import artist as martist, cbook, transforms as mtransforms
3+
from matplotlib import (
4+
artist as martist, cbook, image as mimage, transforms as mtransforms)
45
from matplotlib.axes import subplot_class_factory
56
from matplotlib.transforms import Bbox
67
from .mpl_axes import Axes
@@ -11,10 +12,18 @@
1112
class ParasiteAxesBase:
1213

1314
def get_images_artists(self):
14-
artists = {a for a in self.get_children() if a.get_visible()}
15-
images = {a for a in self.images if a.get_visible()}
15+
artists = []
16+
images = []
1617

17-
return list(images), list(artists - images)
18+
for a in self.get_children():
19+
if not a.get_visible():
20+
continue
21+
if isinstance(a, mimage.AxesImage):
22+
images.append(a)
23+
else:
24+
artists.append(a)
25+
26+
return images, artists
1827

1928
def __init__(self, parent_axes, **kwargs):
2029
self._parent_axes = parent_axes
@@ -221,7 +230,7 @@ def _get_legend_handles(self, legend_handler_map=None):
221230
def draw(self, renderer):
222231

223232
orig_artists = list(self.artists)
224-
orig_images = list(self.images)
233+
orig_children_len = len(self._children)
225234

226235
if hasattr(self, "get_axes_locator"):
227236
locator = self.get_axes_locator()
@@ -239,12 +248,12 @@ def draw(self, renderer):
239248
for ax in self.parasites:
240249
ax.apply_aspect(rect)
241250
images, artists = ax.get_images_artists()
242-
self.images.extend(images)
251+
self._children.extend(images)
243252
self.artists.extend(artists)
244253

245254
super().draw(renderer)
246255
self.artists = orig_artists
247-
self.images = orig_images
256+
self._children = self._children[:orig_children_len]
248257

249258
def cla(self):
250259
for ax in self.parasites:

0 commit comments

Comments
 (0)