Skip to content

Commit

Permalink
Alternate implementation of lazy ticks.
Browse files Browse the repository at this point in the history
Speeds up creation of 10x10 subplots ~4-fold (from 2.7s to 0.7s).
  • Loading branch information
anntzer committed Jan 24, 2018
1 parent 6044a57 commit ce2ddee
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,29 @@ class Ticker(object):
formatter = None


class _LazyTickList(object):
"""
A descriptor for lazy instantiation of tick lists.
See comment above definition of the ``majorTicks`` and ``minorTicks``
attributes.
"""

def __init__(self, major):
self._major = major

def __get__(self, instance, cls):
if instance is None:
return self
else:
if self._major:
instance.majorTicks = [instance._get_tick(major=True)]
return instance.majorTicks
else:
instance.minorTicks = [instance._get_tick(major=False)]
return instance.minorTicks


class Axis(artist.Artist):
"""
Public attributes
Expand Down Expand Up @@ -696,8 +719,6 @@ def __init__(self, axes, pickradius=15):
self.label = self._get_label()
self.labelpad = rcParams['axes.labelpad']
self.offsetText = self._get_offset_text()
self.majorTicks = []
self.minorTicks = []
self.unit_data = None
self.pickradius = pickradius

Expand All @@ -708,6 +729,12 @@ def __init__(self, axes, pickradius=15):
self.cla()
self._set_scale('linear')

# During initialization, Axis objects often create ticks that are later
# unused; this turns out to be a very slow step. Instead, use a custom
# descriptor to make the tick lists lazy and instantiate them as needed.
majorTicks = _LazyTickList(major=True)
minorTicks = _LazyTickList(major=False)

def set_label_coords(self, x, y, transform=None):
"""
Set the coordinates of the label. By default, the x
Expand Down Expand Up @@ -798,11 +825,15 @@ def reset_ticks(self):
Each list starts with a single fresh Tick.
"""
del self.majorTicks[:]
del self.minorTicks[:]

self.majorTicks.extend([self._get_tick(major=True)])
self.minorTicks.extend([self._get_tick(major=False)])
# Restore the lazy tick lists.
try:
del self.majorTicks
except AttributeError:
pass
try:
del self.minorTicks
except AttributeError:
pass
self._lastNumMajorTicks = 1
self._lastNumMinorTicks = 1

Expand Down

0 comments on commit ce2ddee

Please sign in to comment.