|
21 | 21 | import matplotlib.units as munits
|
22 | 22 | import numpy as np
|
23 | 23 | import warnings
|
| 24 | +import itertools |
24 | 25 |
|
25 | 26 | GRIDLINE_INTERPOLATION_STEPS = 180
|
26 | 27 |
|
@@ -625,6 +626,42 @@ class Ticker(object):
|
625 | 626 | formatter = None
|
626 | 627 |
|
627 | 628 |
|
| 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 | + |
628 | 665 | class Axis(artist.Artist):
|
629 | 666 | """
|
630 | 667 | Public attributes
|
@@ -781,13 +818,12 @@ def reset_ticks(self):
|
781 | 818 | # build a few default ticks; grow as necessary later; only
|
782 | 819 | # define 1 so properties set on ticks will be copied as they
|
783 | 820 | # 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) |
791 | 827 |
|
792 | 828 | def set_tick_params(self, which='major', reset=False, **kw):
|
793 | 829 | """
|
@@ -872,7 +908,8 @@ def _translate_tick_kw(kw, to_init_kw=True):
|
872 | 908 |
|
873 | 909 | def set_clip_path(self, clippath, transform=None):
|
874 | 910 | 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)): |
876 | 913 | child.set_clip_path(clippath, transform)
|
877 | 914 | self.stale = True
|
878 | 915 |
|
|
0 commit comments