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

Skip to content

Commit 21a80f3

Browse files
committed
WIP : made Container inherit from Artist
- add broadcasting draw method to Container - special-case BarContainer __new__ - remove duplicate functions from Container
1 parent 6f307f8 commit 21a80f3

File tree

2 files changed

+23
-78
lines changed

2 files changed

+23
-78
lines changed

lib/matplotlib/artist.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ def get_rasterized(self):
673673
"return True if the artist is to be rasterized"
674674
return self._rasterized
675675

676+
def set_remove_method(self, f):
677+
self._remove_method = f
678+
676679
def set_rasterized(self, rasterized):
677680
"""
678681
Force rasterized (bitmap) drawing in vector backend output.

lib/matplotlib/container.py

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
unicode_literals)
33

44
import six
5-
5+
from matplotlib.artist import Artist
66
import matplotlib.cbook as cbook
77

88

9-
class Container(tuple):
9+
class Container(tuple, Artist):
1010
"""
1111
Base class for containers.
1212
"""
@@ -17,92 +17,34 @@ def __repr__(self):
1717
def __new__(cls, *kl, **kwargs):
1818
return tuple.__new__(cls, kl[0])
1919

20-
def __init__(self, kl, label=None):
21-
22-
self.eventson = False # fire events only if eventson
23-
self._oid = 0 # an observer id
24-
self._propobservers = {} # a dict from oids to funcs
25-
26-
self._remove_method = None
27-
28-
self.set_label(label)
29-
30-
def set_remove_method(self, f):
31-
self._remove_method = f
20+
def __init__(self, kl, label=None, **kwargs):
21+
Artist.__init__(self, **kwargs)
22+
self.set_label(label=label)
3223

3324
def remove(self):
25+
# remove the children
3426
for c in self:
3527
c.remove()
36-
37-
if self._remove_method:
38-
self._remove_method(self)
39-
40-
def __getstate__(self):
41-
d = self.__dict__.copy()
42-
# remove the unpicklable remove method, this will get re-added on load
43-
# (by the axes) if the artist lives on an axes.
44-
d['_remove_method'] = None
45-
return d
46-
47-
def get_label(self):
48-
"""
49-
Get the label used for this artist in the legend.
50-
"""
51-
return self._label
52-
53-
def set_label(self, s):
54-
"""
55-
Set the label to *s* for auto legend.
56-
57-
ACCEPTS: string or anything printable with '%s' conversion.
58-
"""
59-
if s is not None:
60-
self._label = '%s' % (s, )
61-
else:
62-
self._label = None
63-
self.pchanged()
64-
65-
def add_callback(self, func):
66-
"""
67-
Adds a callback function that will be called whenever one of
68-
the :class:`Artist`'s properties changes.
69-
70-
Returns an *id* that is useful for removing the callback with
71-
:meth:`remove_callback` later.
72-
"""
73-
oid = self._oid
74-
self._propobservers[oid] = func
75-
self._oid += 1
76-
return oid
77-
78-
def remove_callback(self, oid):
79-
"""
80-
Remove a callback based on its *id*.
81-
82-
.. seealso::
83-
84-
:meth:`add_callback`
85-
For adding callbacks
86-
87-
"""
88-
try:
89-
del self._propobservers[oid]
90-
except KeyError:
91-
pass
92-
93-
def pchanged(self):
94-
"""
95-
Fire an event when property changed, calling all of the
96-
registered callbacks.
97-
"""
98-
for oid, func in list(six.iteritems(self._propobservers)):
99-
func(self)
28+
# call up to the Artist remove method
29+
super(Container, self).remove(self)
10030

10131
def get_children(self):
10232
return list(cbook.flatten(self))
10333

34+
def draw(self, renderer, *args, **kwargs):
35+
# just broadcast the draw down to children
36+
for a in self:
37+
a.draw(renderer, *args, **kwargs)
38+
10439

10540
class BarContainer(Container):
41+
def __new__(cls, patches, errorbar=None, **kwargs):
42+
if errorbar is None:
43+
errorbar = tuple()
44+
else:
45+
errorbar = tuple(errorbar)
46+
patches = tuple(patches)
47+
return super(BarContainer, cls).__new__(patches + errorbar, **kwargs)
10648

10749
def __init__(self, patches, errorbar=None, **kwargs):
10850
self.patches = patches

0 commit comments

Comments
 (0)