X Tutup
Skip to content
Merged
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
46 changes: 38 additions & 8 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,12 +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
try:
self.set_clip_path(self.axes.patch)
except AttributeError:
Expand Down
X Tutup