-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Lazy initialization of axis default tick list #9727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import matplotlib.units as munits | ||
import numpy as np | ||
import warnings | ||
import itertools | ||
|
||
GRIDLINE_INTERPOLATION_STEPS = 180 | ||
|
||
|
@@ -625,6 +626,42 @@ class Ticker(object): | |
formatter = None | ||
|
||
|
||
class _LazyDefaultTickList(object): | ||
""" | ||
A lazy evaluating placeholder for the default tick list. | ||
|
||
On access the class replaces itself with the default 1-element list. We | ||
use the lazy evaluation so that :meth:`matplotlib.axis.Axis.reset_ticks` | ||
can be called multiple times without the overhead of initialzing every | ||
time. | ||
|
||
https://github.com/matplotlib/matplotlib/issues/6664 | ||
""" | ||
def __init__(self, axis, major=True): | ||
self.axis = axis | ||
self.major = major | ||
|
||
def _instantiate_list(self): | ||
if self.major: | ||
self.axis.majorTicks = [self.axis._get_tick(major=True)] | ||
self.axis._lastNumMajorTicks = 1 | ||
return self.axis.majorTicks | ||
else: | ||
self.axis.minorTicks = [self.axis._get_tick(major=False)] | ||
self.axis._lastNumMinorTicks = 1 | ||
return self.axis.minorTicks | ||
|
||
def __iter__(self): | ||
return iter(self._instantiate_list()) | ||
|
||
def __len__(self): | ||
return len(self._instantiate_list()) | ||
|
||
def __getitem__(self, key): | ||
l = self._instantiate_list() | ||
return l[key] | ||
|
||
|
||
class Axis(artist.Artist): | ||
""" | ||
Public attributes | ||
|
@@ -781,13 +818,12 @@ def reset_ticks(self): | |
# build a few default ticks; grow as necessary later; only | ||
# define 1 so properties set on ticks will be copied as they | ||
# grow | ||
del self.majorTicks[:] | ||
del self.minorTicks[:] | ||
|
||
self.majorTicks.extend([self._get_tick(major=True)]) | ||
self.minorTicks.extend([self._get_tick(major=False)]) | ||
self._lastNumMajorTicks = 1 | ||
self._lastNumMinorTicks = 1 | ||
if not isinstance(self.majorTicks, _LazyDefaultTickList): | ||
del self.majorTicks[:] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You shouldn't need this any more since the list is being removed, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guess you're right. I was wondering anyway, why the previous code bothered to del every item from the list and not just created a new list ( Also, can you tell if there's still a need for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it worked this way so that any use of |
||
self.majorTicks = _LazyDefaultTickList(self, major=True) | ||
if not isinstance(self.minorTicks, _LazyDefaultTickList): | ||
del self.minorTicks[:] | ||
self.minorTicks = _LazyDefaultTickList(self, major=False) | ||
|
||
def set_tick_params(self, which='major', reset=False, **kw): | ||
""" | ||
|
@@ -872,7 +908,8 @@ def _translate_tick_kw(kw, to_init_kw=True): | |
|
||
def set_clip_path(self, clippath, transform=None): | ||
artist.Artist.set_clip_path(self, clippath, transform) | ||
for child in self.majorTicks + self.minorTicks: | ||
for child in itertools.chain(iter(self.majorTicks), | ||
iter(self.minorTicks)): | ||
child.set_clip_path(clippath, transform) | ||
self.stale = True | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to consider that checking the git blame is sufficient to see where this comes from, and prefer not adding explicit links to issues (otherwise it's a bit endless). Not a huge deal of course.