@@ -662,6 +662,22 @@ class Ticker(object):
662
662
formatter = None
663
663
664
664
665
+ class _LazyTickList (object ):
666
+ def __init__ (self , major ):
667
+ self ._major = major
668
+
669
+ def __get__ (self , instance , cls ):
670
+ if instance is None :
671
+ return self
672
+ else :
673
+ if self ._major :
674
+ instance .majorTicks = [instance ._get_tick (major = True )]
675
+ return instance .majorTicks
676
+ else :
677
+ instance .minorTicks = [instance ._get_tick (major = False )]
678
+ return instance .minorTicks
679
+
680
+
665
681
class Axis (artist .Artist ):
666
682
"""
667
683
Public attributes
@@ -696,8 +712,6 @@ def __init__(self, axes, pickradius=15):
696
712
self .label = self ._get_label ()
697
713
self .labelpad = rcParams ['axes.labelpad' ]
698
714
self .offsetText = self ._get_offset_text ()
699
- self .majorTicks = []
700
- self .minorTicks = []
701
715
self .unit_data = None
702
716
self .pickradius = pickradius
703
717
@@ -708,6 +722,12 @@ def __init__(self, axes, pickradius=15):
708
722
self .cla ()
709
723
self ._set_scale ('linear' )
710
724
725
+ # During initialization, Axis objects often create ticks that are later
726
+ # unused; this turns out to be a very slow step. Instead, use a custom
727
+ # descriptor to make the tick lists lazy and instantiate them as needed.
728
+ majorTicks = _LazyTickList (major = True )
729
+ minorTicks = _LazyTickList (major = False )
730
+
711
731
def set_label_coords (self , x , y , transform = None ):
712
732
"""
713
733
Set the coordinates of the label. By default, the x
@@ -798,11 +818,15 @@ def reset_ticks(self):
798
818
799
819
Each list starts with a single fresh Tick.
800
820
"""
801
- del self .majorTicks [:]
802
- del self .minorTicks [:]
803
-
804
- self .majorTicks .extend ([self ._get_tick (major = True )])
805
- self .minorTicks .extend ([self ._get_tick (major = False )])
821
+ # Restore the lazy tick lists.
822
+ try :
823
+ del self .majorTicks
824
+ except AttributeError :
825
+ pass
826
+ try :
827
+ del self .minorTicks
828
+ except AttributeError :
829
+ pass
806
830
self ._lastNumMajorTicks = 1
807
831
self ._lastNumMinorTicks = 1
808
832
0 commit comments