Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5901e0e

Browse files
author
y1thof
committed
lazy initialization of axis default tick list
1 parent ade25fb commit 5901e0e

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

lib/matplotlib/axis.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import matplotlib.units as munits
2222
import numpy as np
2323
import warnings
24+
import itertools
2425

2526
GRIDLINE_INTERPOLATION_STEPS = 180
2627

@@ -625,6 +626,42 @@ class Ticker(object):
625626
formatter = None
626627

627628

629+
class LazyDefaultTickList(object):
630+
"""
631+
A lazy evaluating placeholder for the default tick list.
632+
633+
On access the class replaces itself with the default 1-element list. We
634+
use the lazy evaluation so that :meth:`matplotlib.axis.Axis.reset_ticks`
635+
can be called multiple times without the overhead of initialzing every
636+
time.
637+
638+
https://github.com/matplotlib/matplotlib/issues/6664
639+
"""
640+
def __init__(self, axis, major=True):
641+
self.axis = axis
642+
self.major = major
643+
644+
def _instantiate_list(self):
645+
if self.major:
646+
self.axis.majorTicks = [self.axis._get_tick(major=True)]
647+
self.axis._lastNumMajorTicks = 1
648+
return self.axis.majorTicks
649+
else:
650+
self.axis.minorTicks = [self.axis._get_tick(major=False)]
651+
self.axis._lastNumMinorTicks = 1
652+
return self.axis.minorTicks
653+
654+
def __iter__(self):
655+
return iter(self._instantiate_list())
656+
657+
def __len__(self):
658+
return len(self._instantiate_list())
659+
660+
def __getitem__(self, key):
661+
l = self._instantiate_list()
662+
return l[key]
663+
664+
628665
class Axis(artist.Artist):
629666
"""
630667
Public attributes
@@ -781,13 +818,12 @@ def reset_ticks(self):
781818
# build a few default ticks; grow as necessary later; only
782819
# define 1 so properties set on ticks will be copied as they
783820
# grow
784-
del self.majorTicks[:]
785-
del self.minorTicks[:]
786-
787-
self.majorTicks.extend([self._get_tick(major=True)])
788-
self.minorTicks.extend([self._get_tick(major=False)])
789-
self._lastNumMajorTicks = 1
790-
self._lastNumMinorTicks = 1
821+
if not isinstance(self.majorTicks, LazyDefaultTickList):
822+
del self.majorTicks[:]
823+
self.majorTicks = LazyDefaultTickList(self, major=True)
824+
if not isinstance(self.minorTicks, LazyDefaultTickList):
825+
del self.minorTicks[:]
826+
self.minorTicks = LazyDefaultTickList(self, major=False)
791827

792828
def set_tick_params(self, which='major', reset=False, **kw):
793829
"""
@@ -872,7 +908,8 @@ def _translate_tick_kw(kw, to_init_kw=True):
872908

873909
def set_clip_path(self, clippath, transform=None):
874910
artist.Artist.set_clip_path(self, clippath, transform)
875-
for child in self.majorTicks + self.minorTicks:
911+
for child in itertools.chain(iter(self.majorTicks),
912+
iter(self.minorTicks)):
876913
child.set_clip_path(clippath, transform)
877914
self.stale = True
878915

0 commit comments

Comments
 (0)