8000 WIP : major Axes refactor by tacaswell · Pull Request #3944 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

WIP : major Axes refactor #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP : made Container inherit from Artist
 - add broadcasting draw method to Container
 - special-case BarContainer __new__
 - remove duplicate functions from Container
  • Loading branch information
tacaswell committed Mar 8, 2015
commit 6f31a563e09beb1211b525edca6efe77d9910c97
3 changes: 3 additions & 0 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,9 @@ def get_rasterized(self):
"return True if the artist is to be rasterized"
return self._rasterized

def set_remove_method(self, f):
self._remove_method = f

def set_rasterized(self, rasterized):
"""
Force rasterized (bitmap) drawing in vector backend output.
Expand Down
98 changes: 20 additions & 78 deletions lib/matplotlib/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
unicode_literals)

import six

from matplotlib.artist import Artist
import matplotlib.cbook as cbook


class Container(tuple):
class Container(tuple, Artist):
"""
Base class for containers.
"""
Expand All @@ -17,92 +17,34 @@ def __repr__(self):
def __new__(cls, *kl, **kwargs):
return tuple.__new__(cls, kl[0])

def __init__(self, kl, label=None):

self.eventson = False # fire events only if eventson
self._oid = 0 # an observer id
self._propobservers = {} # a dict from oids to funcs

self._remove_method = None

self.set_label(label)

def set_remove_method(self, f):
self._remove_method = f
def __init__(self, kl, label=None, **kwargs):
Artist.__init__(self, **kwargs)
self.set_label(label=label)

def remove(self):
# remove the children
for c in self:
c.remove()

if self._remove_method:
self._remove_method(self)

def __getstate__(self):
d = self.__dict__.copy()
# remove the unpicklable remove method, this will get re-added on load
# (by the axes) if the artist lives on an axes.
d['_remove_method'] = None
return d

def get_label(self):
"""
Get the label used for this artist in the legend.
"""
return self._label

def set_label(self, s):
"""
Set the label to *s* for auto legend.

ACCEPTS: string or anything printable with '%s' conversion.
"""
if s is not None:
self._label = '%s' % (s, )
else:
self._label = None
self.pchanged()

def add_callback(self, func):
"""
Adds a callback function that will be called whenever one of
the :class:`Artist`'s properties changes.

Returns an *id* that is useful for removing the callback with
:meth:`remove_callback` later.
"""
oid = self._oid
self._propobservers[oid] = func
self._oid += 1
return oid

def remove_callback(self, oid):
"""
Remove a callback based on its *id*.

.. seealso::

:meth:`add_callback`
For adding callbacks

"""
try:
del self._propobservers[oid]
except KeyError:
pass

def pchanged(self):
"""
Fire an event when property changed, calling all of the
registered callbacks.
"""
for oid, func in list(six.iteritems(self._propobservers)):
func(self)
# call up to the Artist remove method
super(Container, self).remove(self)

def get_children(self):
return list(cbook.flatten(self))

def draw(self, renderer, *args, **kwargs):
# just broadcast the draw down to children
for a in self:
a.draw(renderer, *args, **kwargs)


class BarContainer(Container):
def __new__(cls, patches, errorbar=None, **kwargs):
if errorbar is None:
errorbar = tuple()
else:
errorbar = tuple(errorbar)
patches = tuple(patches)
return super(BarContainer, cls).__new__(patches + errorbar, **kwargs)

def __init__(self, patches, errorbar=None, **kwargs):
self.patches = patches
Expand Down
0