8000 WIP : helper list-class · matplotlib/matplotlib@266438d · GitHub
[go: up one dir, main page]

Skip to content

Commit 266438d

Browse files
committed
WIP : helper list-class
Starting ground work for re-factoring Axes to use a single tree for storing the Artists in an Axes.
1 parent f28c7c7 commit 266438d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

lib/matplotlib/artist.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def remove(self):
136136
# protected attribute if Python supported that sort of thing. The
137137
# callback has one parameter, which is the child to be removed.
138138
if self._remove_method is not None:
139+
# set the current axes to None
140+
self._axes = None
141+
# use the call back registered by the axes when the artist
142+
# was added to remove it the artist from the axes
139143
self._remove_method(self)
140144
else:
141145
raise NotImplementedError('cannot remove artist')

lib/matplotlib/cbook.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,6 +2311,70 @@ def get_instancemethod(self):
23112311
return getattr(self.parent_obj, self.instancemethod_name)
23122312

23132313

2314+
_art_list_msg = ("The use of Axes.lines, Axes.patches, Axes.texts "
2315+
"Axes.tables, Axes.artists, Axes.collections, "
2316+
"Axes.containers, "
2317+
"and Axes.images have been "
2318+
"deprecated. All artists are now stored is a single "
2319+
"tree accessible via the Axes.artist_tree property. "
2320+
"Please use the ``remove`` method on the artists to remove"
2321+
"them from an Axes. \n\n"
2322+
"These lists will be removed in 1.7 or 2.0.")
2323+
2324+
2325+
class MPLRemoverList(list):
2326+
"""
2327+
2328+
This is a sub-class of list which implements the logic to manage the
2329+
backwards compatibility during deprecation of the lines, patches,
2330+
texts, tables, artists, images, collections, and containers
2331+
attributes from the Axes class. This will allow users to continue
2332+
to use `ax.lines.pop()` to remove lines from an axes, even though
2333+
the draw method no longer looks at those lists at render time.
2334+
2335+
This class will be removed when the list are.
2336+
2337+
"""
2338+
def __delslice__(self, a, b):
2339+
# warn
2340+
warnings.warn(_art_list_msg, mplDeprecation, stacklevel=1)
2341+
# grab what we will be removing
2342+
res = self[a:b]
2343+
# remove it from this list
2344+
super(MPLRemoverList, self).__delslice__(self, a, b)
2345+
# see if we need to call the real remove
2346+
# Artist.remove sets _axes = None so if this is called
2347+
# remove it won't be called again, but if a user removes
2348+
# an artist from these lists directly, remove will correctly
2349+
# be called.
2350+
for a in res:
2351+
# this works because of details of how Artist.remove works
2352+
if a.axes:
2353+
a.remove()
2354+
2355+
def __delitem__(self, y):
2356+
# see __delslice__ for explanation of logic
2357+
warnings.warn(_art_list_msg, mplDeprecation, stacklevel=1)
2358+
res = self[y]
2359+
super(MPLRemoverList, self).__delitem__(self, y)
2360+
if res.axes:
2361+
res.remove()
2362+
2363+
def pop(self, i):
2364+
# see __delslice__ for explanation of logic
2365+
warnings.warn(_art_list_msg, mplDeprecation, stacklevel=1)
2366+
res = super(MPLRemoverList, self).pop(self, i)
2367+
if res.axes:
2368+
res.remove()
2369+
2370+
def remove(self, item):
2371+
# see __delslice__ for explanation of logic
2372+
warnings.warn(_art_list_msg, mplDeprecation, stacklevel=1)
2373+
res = super(MPLRemoverList, self).remove(self, item)
2374+
if item.axes:
2375+
res.remove()
2376+
2377+
23142378
# Numpy > 1.6.x deprecates putmask in favor of the new copyto.
23152379
# So long as we support versions 1.6.x and less, we need the
23162380
# following local version of putmask. We choose to make a

0 commit comments

Comments
 (0)
0