-
-
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
Closed
timhoffm
wants to merge
3
commits into
matplotlib:master
from
timhoffm:prevent-unnecessary-axis-ticks-creation
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Internal handling of axis ticks optimized | ||
----------------------------------------- | ||
|
||
No officially documented parts of the API have changed. However, you may have | ||
implicitly relied on a certain behavior. | ||
|
||
`Axis.majorTicks` and `Axis.minorTicks` are deprecated | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The undocumented attributes `majorTicks` and `minorTicks` of | ||
:class:`matplotlib.axis.Axis` are now explicitly considered private and may be | ||
removed in the future. Please uses :meth:`matplotlib.axis.Axis.get_major_ticks` | ||
and :meth:`matplotlib.axis.Axis.get_minor_ticks` only. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,40 @@ 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)] | ||
return self.axis.majorTicks | ||
else: | ||
self.axis.minorTicks = [self.axis._get_tick(major=False)] | ||
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 +816,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[:] | ||
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 +906,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 | ||
|
||
|
@@ -1339,22 +1374,15 @@ def get_major_ticks(self, numticks=None): | |
numticks = len(self.get_major_locator()()) | ||
if len(self.majorTicks) < numticks: | ||
# update the new tick label properties from the old | ||
protoTick = self.majorTicks[0] | ||
for i in range(numticks - len(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. could even just replace the outer "if len(self.majorTicks) < numticks" by "while len() < numticks" and remove one indent level. |
||
tick = self._get_tick(major=True) | ||
self.majorTicks.append(tick) | ||
|
||
if self._lastNumMajorTicks < numticks: | ||
protoTick = self.majorTicks[0] | ||
for i in range(self._lastNumMajorTicks, len(self.majorTicks)): | ||
tick = self.majorTicks[i] | ||
if self._gridOnMajor: | ||
tick.gridOn = True | ||
self._copy_tick_props(protoTick, tick) | ||
self.majorTicks.append(tick) | ||
|
||
self._lastNumMajorTicks = numticks | ||
ticks = self.majorTicks[:numticks] | ||
|
||
return ticks | ||
return self.majorTicks[:numticks] | ||
|
||
def get_minor_ticks(self, numticks=None): | ||
'get the minor tick instances; grow as necessary' | ||
|
@@ -1363,22 +1391,15 @@ def get_minor_ticks(self, numticks=None): | |
|
||
if len(self.minorTicks) < numticks: | ||
# update the new tick label properties from the old | ||
protoTick = self.minorTicks[0] | ||
for i in range(numticks - len(self.minorTicks)): | ||
tick = self._get_tick(major=False) | ||
self.minorTicks.append(tick) | ||
|
||
if self._lastNumMinorTicks < numticks: | ||
protoTick = self.minorTicks[0] | ||
for i in range(self._lastNumMinorTicks, len(self.minorTicks)): | ||
tick = self.minorTicks[i] | ||
if self._gridOnMinor: | ||
tick.gridOn = True | ||
self._copy_tick_props(protoTick, tick) | ||
self.minorTicks.append(tick) | ||
|
||
self._lastNumMinorTicks = numticks | ||
ticks = self.minorTicks[:numticks] | ||
|
||
return ticks | ||
return self.minorTicks[:numticks] | ||
|
||
def grid(self, b=None, which='major', **kwargs): | ||
""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.