Skip to content

Commit 1636bed

Browse files
authored
Merge pull request #8626 from QuLogic/Axes-init-speedup
PERF: Axes.__init__ speedup
2 parents 5cbe37f + 4801cbc commit 1636bed

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

lib/matplotlib/axes/_base.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ def __init__(self, fig, rect,
481481
""" % {'scale': ' | '.join(
482482
[repr(x) for x in mscale.get_scale_names()])}
483483
martist.Artist.__init__(self)
484+
self._in_init = True
484485
if isinstance(rect, mtransforms.Bbox):
485486
self._position = rect
486487
else:
@@ -578,6 +579,8 @@ def __init__(self, fig, rect,
578579
right=rcParams['ytick.right'] and rcParams['ytick.major.right'],
579580
which='major')
580581

582+
self._in_init = False
583+
581584
def __getstate__(self):
582585
# The renderer should be re-created by the figure, and then cached at
583586
# that point.
@@ -609,11 +612,11 @@ def get_window_extent(self, *args, **kwargs):
609612
def _init_axis(self):
610613
"move this out of __init__ because non-separable axes don't use it"
611614
self.xaxis = maxis.XAxis(self)
612-
self.spines['bottom'].register_axis(self.xaxis)
613-
self.spines['top'].register_axis(self.xaxis)
615+
self.spines['bottom'].register_axis(self.xaxis, _init=True)
616+
self.spines['top'].register_axis(self.xaxis, _init=True)
614617
self.yaxis = maxis.YAxis(self)
615-
self.spines['left'].register_axis(self.yaxis)
616-
self.spines['right'].register_axis(self.yaxis)
618+
self.spines['left'].register_axis(self.yaxis, _init=True)
619+
self.spines['right'].register_axis(self.yaxis, _init=True)
617620
self._update_transScale()
618621

619622
def set_figure(self, fig):
@@ -977,10 +980,13 @@ def cla(self):
977980
xaxis_visible = self.xaxis.get_visible()
978981
yaxis_visible = self.yaxis.get_visible()
979982

980-
self.xaxis.cla()
981-
self.yaxis.cla()
982-
for name, spine in six.iteritems(self.spines):
983-
spine.cla()
983+
# Don't clear during __init__ because they're already been cleared by
984+
# their own __init__.
985+
if not self._in_init:
986+
self.xaxis.cla()
987+
self.yaxis.cla()
988+
for name, spine in six.iteritems(self.spines):
989+
spine.cla()
984990

985991
self.ignore_existing_data_limits = True
986992
self.callbacks = cbook.CallbackRegistry()

lib/matplotlib/projections/polar.py

+2
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,10 @@ def __init__(self, *args, **kwargs):
761761
kwargs.pop('rlabel_position', 22.5))
762762

763763
Axes.__init__(self, *args, **kwargs)
764+
self._in_init = True
764765
self.set_aspect('equal', adjustable='box', anchor='C')
765766
self.cla()
767+
self._in_init = False
766768
__init__.__doc__ = Axes.__init__.__doc__
767769

768770
def cla(self):

lib/matplotlib/spines.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,17 @@ def _ensure_position_is_set(self):
164164
self._position = ('outward', 0.0) # in points
165165
self.set_position(self._position)
166166

167-
def register_axis(self, axis):
167+
def register_axis(self, axis, _init=False):
168168
"""register an axis
169169
170170
An axis should be registered with its corresponding spine from
171171
the Axes instance. This allows the spine to clear any axis
172172
properties when needed.
173173
"""
174174
self.axis = axis
175-
if self.axis is not None:
175+
if not _init and self.axis is not None:
176+
# Clear the axis when added, but *not* if the caller says it was
177+
# just created.
176178
self.axis.cla()
177179
self.stale = True
178180

0 commit comments

Comments
 (0)