Skip to content

Axes.__init__ speedup #8626

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

Merged
merged 3 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 14 additions & 8 deletions lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def __init__(self, fig, rect,
""" % {'scale': ' | '.join(
[repr(x) for x in mscale.get_scale_names()])}
martist.Artist.__init__(self)
self._in_init = True
if isinstance(rect, mtransforms.Bbox):
self._position = rect
else:
Expand Down Expand Up @@ -576,6 +577,8 @@ def __init__(self, fig, rect,
right=rcParams['ytick.right'] and rcParams['ytick.major.right'],
which='major')

self._in_init = False

def __getstate__(self):
# The renderer should be re-created by the figure, and then cached at
# that point.
Expand Down Expand Up @@ -607,11 +610,11 @@ def get_window_extent(self, *args, **kwargs):
def _init_axis(self):
"move this out of __init__ because non-separable axes don't use it"
self.xaxis = maxis.XAxis(self)
self.spines['bottom'].register_axis(self.xaxis)
self.spines['top'].register_axis(self.xaxis)
self.spines['bottom'].register_axis(self.xaxis, _init=True)
self.spines['top'].register_axis(self.xaxis, _init=True)
self.yaxis = maxis.YAxis(self)
self.spines['left'].register_axis(self.yaxis)
self.spines['right'].register_axis(self.yaxis)
self.spines['left'].register_axis(self.yaxis, _init=True)
self.spines['right'].register_axis(self.yaxis, _init=True)
self._update_transScale()

def set_figure(self, fig):
Expand Down Expand Up @@ -965,10 +968,13 @@ def cla(self):
xaxis_visible = self.xaxis.get_visible()
yaxis_visible = self.yaxis.get_visible()

self.xaxis.cla()
self.yaxis.cla()
for name, spine in six.iteritems(self.spines):
spine.cla()
# Don't clear during __init__ because they're already been cleared by
# their own __init__.
if not self._in_init:
self.xaxis.cla()
self.yaxis.cla()
for name, spine in six.iteritems(self.spines):
spine.cla()

self.ignore_existing_data_limits = True
self.callbacks = cbook.CallbackRegistry()
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,10 @@ def __init__(self, *args, **kwargs):
If you need to interpolate data points, consider running
cbook.simple_linear_interpolation on the data before passing to matplotlib.""")
Axes.__init__(self, *args, **kwargs)
self._in_init = True
self.set_aspect('equal', adjustable='box', anchor='C')
self.cla()
self._in_init = False
__init__.__doc__ = Axes.__init__.__doc__

def cla(self):
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/spines.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,17 @@ def _ensure_position_is_set(self):
self._position = ('outward', 0.0) # in points
self.set_position(self._position)

def register_axis(self, axis):
def register_axis(self, axis, _init=False):
"""register an axis

An axis should be registered with its corresponding spine from
the Axes instance. This allows the spine to clear any axis
properties when needed.
"""
self.axis = axis
if self.axis is not None:
if not _init and self.axis is not None:
# Clear the axis when added, but *not* if the caller says it was
# just created.
self.axis.cla()
self.stale = True

Expand Down