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

Skip to content

Commit 8719715

Browse files
committed
improve matplotlib import time by caching ArtistInspector
1 parent 39c997e commit 8719715

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

lib/matplotlib/artist.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import namedtuple
22
import contextlib
3-
from functools import wraps
3+
from functools import lru_cache, wraps
44
import inspect
55
from inspect import Signature, Parameter
66
import logging
@@ -1494,17 +1494,29 @@ def get_setters(self):
14941494
continue
14951495
func = getattr(self.o, name)
14961496
if (not callable(func)
1497-
or len(inspect.signature(func).parameters) < 2
1497+
or self.number_of_parameters(func) < 2
14981498
or self.is_alias(func)):
14991499
continue
15001500
setters.append(name[4:])
15011501
return setters
15021502

1503-
def is_alias(self, o):
1504-
"""Return whether method object *o* is an alias for another method."""
1505-
ds = inspect.getdoc(o)
1503+
@staticmethod
1504+
@lru_cache(maxsize=None)
1505+
def number_of_parameters(func):
1506+
"""Return number of parameters of the callable *func*."""
1507+
return len(inspect.signature(func).parameters)
1508+
1509+
@staticmethod
1510+
@lru_cache(maxsize=None)
1511+
def is_alias(method):
1512+
"""
1513+
Return whether the object *method* is an alias for another method.
1514+
"""
1515+
1516+
ds = inspect.getdoc(method)
15061517
if ds is None:
15071518
return False
1519+
15081520
return ds.startswith('Alias for ')
15091521

15101522
def aliased_name(self, s):

lib/matplotlib/axis.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,11 @@ def clear(self):
895895
896896
This does not reset tick and tick label visibility.
897897
"""
898+
self.label._reset_visual_defaults()
899+
self.offsetText._reset_visual_defaults()
900+
self.labelpad = mpl.rcParams['axes.labelpad']
898901

899-
self.label.set_text('') # self.set_label_text would change isDefault_
902+
self._init()
900903

901904
self._set_scale('linear')
902905

@@ -2193,6 +2196,13 @@ class XAxis(Axis):
21932196

21942197
def __init__(self, *args, **kwargs):
21952198
super().__init__(*args, **kwargs)
2199+
self._init()
2200+
2201+
def _init(self):
2202+
"""
2203+
Initialize the label and offsetText instance values and
2204+
`label_position` / `offset_text_position`.
2205+
"""
21962206
# x in axes coords, y in display coords (to be updated at draw time by
21972207
# _update_label_positions and _update_offset_text_position).
21982208
self.label.set(
@@ -2202,6 +2212,7 @@ def __init__(self, *args, **kwargs):
22022212
self.axes.transAxes, mtransforms.IdentityTransform()),
22032213
)
22042214
self.label_position = 'bottom'
2215+
22052216
self.offsetText.set(
22062217
x=1, y=0,
22072218
verticalalignment='top', horizontalalignment='right',
@@ -2444,6 +2455,13 @@ class YAxis(Axis):
24442455

24452456
def __init__(self, *args, **kwargs):
24462457
super().__init__(*args, **kwargs)
2458+
self._init()
2459+
2460+
def _init(self):
2461+
"""
2462+
Initialize the label and offsetText instance values and
2463+
`label_position` / `offset_text_position`.
2464+
"""
24472465
# x in display coords, y in axes coords (to be updated at draw time by
24482466
# _update_label_positions and _update_offset_text_position).
24492467
self.label.set(

lib/matplotlib/text.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,39 @@ def __init__(self,
164164
super().__init__()
165165
self._x, self._y = x, y
166166
self._text = ''
167+
self._reset_visual_defaults(
168+
text=text,
169+
color=color,
170+
fontproperties=fontproperties,
171+
usetex=usetex,
172+
parse_math=parse_math,
173+
wrap=wrap,
174+
verticalalignment=verticalalignment,
175+
horizontalalignment=horizontalalignment,
176+
multialignment=multialignment,
177+
rotation=rotation,
178+
transform_rotates_text=transform_rotates_text,
179+
linespacing=linespacing,
180+
rotation_mode=rotation_mode,
181+
)
182+
self.update(kwargs)
183+
184+
def _reset_visual_defaults(
185+
self,
186+
text='',
187+
color=None,
188+
fontproperties=None,
189+
usetex=None,
190+
parse_math=None,
191+
wrap=False,
192+
verticalalignment='baseline',
193+
horizontalalignment='left',
194+
multialignment=None,
195+
rotation=None,
196+
transform_rotates_text=False,
197+
linespacing=None,
198+
rotation_mode=None,
199+
):
167200
self.set_text(text)
168201
self.set_color(
169202
color if color is not None else mpl.rcParams["text.color"])
@@ -183,7 +216,6 @@ def __init__(self,
183216
linespacing = 1.2 # Maybe use rcParam later.
184217
self.set_linespacing(linespacing)
185218
self.set_rotation_mode(rotation_mode)
186-
self.update(kwargs)
187219

188220
def update(self, kwargs):
189221
# docstring inherited

0 commit comments

Comments
 (0)