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

Skip to content

Commit f038112

Browse files
committed
Don't import rcParams but rather use mpl.rcParams.
This helps letting modules be imported early in the init process, even before the rcParams object is constructed. I didn't change all cases, just those needed to make matplotlib.scale importable into matplotlib.colors.
1 parent b47fc9d commit f038112

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

doc/devel/contributing.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ rules before submitting a pull request:
215215
import matplotlib.cbook as cbook
216216
import matplotlib.patches as mpatches
217217

218+
In general, Matplotlib modules should **not** import `.rcParams` using ``from
219+
matplotlib import rcParams``, but rather access it as ``mpl.rcParams``. This
220+
is because some modules are imported very early, before the `.rcParams`
221+
singleton is constructed.
222+
218223
* If your change is a major new feature, add an entry to the ``What's new``
219224
section by adding a new file in ``doc/users/next_whats_new`` (see
220225
:file:`doc/users/next_whats_new/README.rst` for more information).

lib/matplotlib/path.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
import numpy as np
1616

17-
from . import _path, cbook, rcParams
17+
import matplotlib as mpl
18+
from . import _path, cbook
1819
from .cbook import _to_unmasked_float_array, simple_linear_interpolation
1920

2021

@@ -181,15 +182,15 @@ def _fast_from_codes_and_verts(cls, verts, codes, internals_from=None):
181182
pth._interpolation_steps = internals_from._interpolation_steps
182183
else:
183184
pth._should_simplify = True
184-
pth._simplify_threshold = rcParams['path.simplify_threshold']
185+
pth._simplify_threshold = mpl.rcParams['path.simplify_threshold']
185186
pth._interpolation_steps = 1
186187
return pth
187188

188189
def _update_values(self):
189-
self._simplify_threshold = rcParams['path.simplify_threshold']
190+
self._simplify_threshold = mpl.rcParams['path.simplify_threshold']
190191
self._should_simplify = (
191192
self._simplify_threshold > 0 and
192-
rcParams['path.simplify'] and
193+
mpl.rcParams['path.simplify'] and
193194
len(self._vertices) >= 128 and
194195
(self._codes is None or np.all(self._codes <= Path.LINETO))
195196
)

lib/matplotlib/scale.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import numpy as np
1414
from numpy import ma
1515

16-
from matplotlib import cbook, docstring, rcParams
16+
import matplotlib as mpl
17+
from matplotlib import cbook, docstring
1718
from matplotlib.ticker import (
1819
NullFormatter, ScalarFormatter, LogFormatterSciNotation, LogitFormatter,
1920
NullLocator, LogLocator, AutoLocator, AutoMinorLocator,
@@ -108,8 +109,8 @@ def set_default_locators_and_formatters(self, axis):
108109
axis.set_major_formatter(ScalarFormatter())
109110
axis.set_minor_formatter(NullFormatter())
110111
# update the minor locator for x and y axis based on rcParams
111-
if (axis.axis_name == 'x' and rcParams['xtick.minor.visible']
112-
or axis.axis_name == 'y' and rcParams['ytick.minor.visible']):
112+
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible']
113+
or axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
113114
axis.set_minor_locator(AutoMinorLocator())
114115
else:
115116
axis.set_minor_locator(NullLocator())
@@ -193,8 +194,8 @@ def set_default_locators_and_formatters(self, axis):
193194
axis.set_major_formatter(ScalarFormatter())
194195
axis.set_minor_formatter(NullFormatter())
195196
# update the minor locator for x and y axis based on rcParams
196-
if (axis.axis_name == 'x' and rcParams['xtick.minor.visible']
197-
or axis.axis_name == 'y' and rcParams['ytick.minor.visible']):
197+
if (axis.axis_name == 'x' and mpl.rcParams['xtick.minor.visible']
198+
or axis.axis_name == 'y' and mpl.rcParams['ytick.minor.visible']):
198199
axis.set_minor_locator(AutoMinorLocator())
199200
else:
200201
axis.set_minor_locator(NullLocator())

lib/matplotlib/ticker.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@
168168
import logging
169169
import locale
170170
import math
171+
171172
import numpy as np
172-
from matplotlib import rcParams
173+
174+
import matplotlib as mpl
173175
from matplotlib import cbook
174176
from matplotlib import transforms as mtransforms
175177

@@ -291,7 +293,7 @@ def fix_minus(s):
291293
:rc:`axes.unicode_minus`.
292294
"""
293295
return (s.replace('-', '\N{MINUS SIGN}')
294-
if rcParams['axes.unicode_minus']
296+
if mpl.rcParams['axes.unicode_minus']
295297
else s)
296298

297299
def _set_locator(self, locator):
@@ -515,19 +517,20 @@ def __init__(self, useOffset=None, useMathText=None, useLocale=None):
515517
# and scientific notation in mathtext
516518

517519
if useOffset is None:
518-
useOffset = rcParams['axes.formatter.useoffset']
519-
self._offset_threshold = rcParams['axes.formatter.offset_threshold']
520+
useOffset = mpl.rcParams['axes.formatter.useoffset']
521+
self._offset_threshold = \
522+
mpl.rcParams['axes.formatter.offset_threshold']
520523
self.set_useOffset(useOffset)
521-
self._usetex = rcParams['text.usetex']
524+
self._usetex = mpl.rcParams['text.usetex']
522525
if useMathText is None:
523-
useMathText = rcParams['axes.formatter.use_mathtext']
526+
useMathText = mpl.rcParams['axes.formatter.use_mathtext']
524527
self.set_useMathText(useMathText)
525528
self.orderOfMagnitude = 0
526529
self.format = ''
527530
self._scientific = True
528-
self._powerlimits = rcParams['axes.formatter.limits']
531+
self._powerlimits = mpl.rcParams['axes.formatter.limits']
529532
if useLocale is None:
530-
useLocale = rcParams['axes.formatter.use_locale']
533+
useLocale = mpl.rcParams['axes.formatter.use_locale']
531534
self._useLocale = useLocale
532535

533536
def get_useOffset(self):
@@ -548,7 +551,7 @@ def get_useLocale(self):
548551

549552
def set_useLocale(self, val):
550553
if val is None:
551-
self._useLocale = rcParams['axes.formatter.use_locale']
554+
self._useLocale = mpl.rcParams['axes.formatter.use_locale']
552555
else:
553556
self._useLocale = val
554557

@@ -559,7 +562,7 @@ def get_useMathText(self):
559562

560563
def set_useMathText(self, val):
561564
if val is None:
562-
self._useMathText = rcParams['axes.formatter.use_mathtext']
565+
self._useMathText = mpl.rcParams['axes.formatter.use_mathtext']
563566
else:
564567
self._useMathText = val
565568

@@ -882,7 +885,7 @@ def __init__(self, base=10.0, labelOnlyBase=False,
882885
self._base = float(base)
883886
self.labelOnlyBase = labelOnlyBase
884887
if minor_thresholds is None:
885-
if rcParams['_internal.classic_mode']:
888+
if mpl.rcParams['_internal.classic_mode']:
886889
minor_thresholds = (0, 0)
887890
else:
888891
minor_thresholds = (1, 0.4)
@@ -1075,8 +1078,8 @@ def __call__(self, x, pos=None):
10751078
10761079
The position *pos* is ignored.
10771080
"""
1078-
usetex = rcParams['text.usetex']
1079-
min_exp = rcParams['axes.formatter.min_exponent']
1081+
usetex = mpl.rcParams['text.usetex']
1082+
min_exp = mpl.rcParams['axes.formatter.min_exponent']
10801083

10811084
if x == 0: # Symlog
10821085
return r'$\mathdefault{0}$'
@@ -1400,7 +1403,7 @@ def get_usetex(self):
14001403

14011404
def set_usetex(self, val):
14021405
if val is None:
1403-
self._usetex = rcParams['text.usetex']
1406+
self._usetex = mpl.rcParams['text.usetex']
14041407
else:
14051408
self._usetex = val
14061409

@@ -1411,7 +1414,7 @@ def get_useMathText(self):
14111414

14121415
def set_useMathText(self, val):
14131416
if val is None:
1414-
self._useMathText = rcParams['axes.formatter.use_mathtext']
1417+
self._useMathText = mpl.rcParams['axes.formatter.use_mathtext']
14151418
else:
14161419
self._useMathText = val
14171420

@@ -1580,7 +1583,7 @@ def symbol(self):
15801583
symbol = self._symbol
15811584
if not symbol:
15821585
symbol = ''
1583-
elif rcParams['text.usetex'] and not self._is_latex:
1586+
elif mpl.rcParams['text.usetex'] and not self._is_latex:
15841587
# Source: http://www.personal.ceu.hu/tex/specchar.htm
15851588
# Backslash must be first for this to work correctly since
15861589
# it keeps getting added in
@@ -1876,7 +1879,7 @@ def view_limits(self, vmin, vmax):
18761879
vmin -= 1
18771880
vmax += 1
18781881

1879-
if rcParams['axes.autolimit_mode'] == 'round_numbers':
1882+
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
18801883
exponent, remainder = divmod(
18811884
math.log10(vmax - vmin), math.log10(max(self.numticks - 1, 1)))
18821885
exponent -= (remainder < .5)
@@ -1919,7 +1922,7 @@ def view_limits(self, dmin, dmax):
19191922
Set the view limits to the nearest multiples of base that
19201923
contain the data.
19211924
"""
1922-
if rcParams['axes.autolimit_mode'] == 'round_numbers':
1925+
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
19231926
vmin = self._edge.le(dmin) * self._edge.step
19241927
vmax = self._edge.ge(dmax) * self._edge.step
19251928
if vmin == vmax:
@@ -2145,7 +2148,7 @@ def _raw_ticks(self, vmin, vmax):
21452148
istep = np.nonzero(steps >= raw_step)[0][0]
21462149

21472150
# Classic round_numbers mode may require a larger step.
2148-
if rcParams['axes.autolimit_mode'] == 'round_numbers':
2151+
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
21492152
for istep in range(istep, len(steps)):
21502153
step = steps[istep]
21512154
best_vmin = (_vmin // step) * step
@@ -2205,7 +2208,7 @@ def view_limits(self, dmin, dmax):
22052208
dmin, dmax = mtransforms.nonsingular(
22062209
dmin, dmax, expander=1e-12, tiny=1e-13)
22072210

2208-
if rcParams['axes.autolimit_mode'] == 'round_numbers':
2211+
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
22092212
return self._raw_ticks(dmin, dmax)[[0, -1]]
22102213
else:
22112214
return dmin, dmax
@@ -2317,7 +2320,7 @@ def __init__(self, base=10.0, subs=(1.0,), numdecs=4, numticks=None):
23172320
23182321
"""
23192322
if numticks is None:
2320-
if rcParams['_internal.classic_mode']:
2323+
if mpl.rcParams['_internal.classic_mode']:
23212324
numticks = 15
23222325
else:
23232326
numticks = 'auto'
@@ -2420,7 +2423,7 @@ def tick_values(self, vmin, vmax):
24202423

24212424
# Get decades between major ticks.
24222425
stride = (max(math.ceil(numdec / (numticks - 1)), 1)
2423-
if rcParams['_internal.classic_mode'] else
2426+
if mpl.rcParams['_internal.classic_mode'] else
24242427
(numdec + 1) // numticks + 1)
24252428

24262429
# Does subs include anything other than 1? Essentially a hack to know
@@ -2469,7 +2472,7 @@ def view_limits(self, vmin, vmax):
24692472
vmax = math.ceil(math.log(vmax) / math.log(b))
24702473
vmin = b ** (vmax - self.numdecs)
24712474

2472-
if rcParams['axes.autolimit_mode'] == 'round_numbers':
2475+
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
24732476
vmin = _decade_less_equal(vmin, self._base)
24742477
vmax = _decade_greater_equal(vmax, self._base)
24752478

@@ -2627,7 +2630,7 @@ def view_limits(self, vmin, vmax):
26272630
if vmax < vmin:
26282631
vmin, vmax = vmax, vmin
26292632

2630-
if rcParams['axes.autolimit_mode'] == 'round_numbers':
2633+
if mpl.rcParams['axes.autolimit_mode'] == 'round_numbers':
26312634
vmin = _decade_less_equal(vmin, b)
26322635
vmax = _decade_greater_equal(vmax, b)
26332636
if vmin == vmax:
@@ -2797,7 +2800,7 @@ def __init__(self):
27972800
To know the values of the non-public parameters, please have a
27982801
look to the defaults of `~matplotlib.ticker.MaxNLocator`.
27992802
"""
2800-
if rcParams['_internal.classic_mode']:
2803+
if mpl.rcParams['_internal.classic_mode']:
28012804
nbins = 9
28022805
steps = [1, 2, 5, 10]
28032806
else:

0 commit comments

Comments
 (0)