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

Skip to content

Commit a6a838d

Browse files
committed
Get default params from matplotlibrc.template.
This makes matplotlibrc.template a valid style file representing the default style (modulo things that styles do not control, such as the backend), avoids having to duplicate the defaults in rcsetup, removes the need to test that the default matplotlibrc is up to date (if not, matplotlib fails to import), and makes it easier to fit lines within 79 characters in rcsetup.py. The only tricky points are: - datapath is not really an rcparam anyways, so just hack `__getitem__` to always return the correct value. - the entry for path.effects was incorrect, as [] would be validated as the "[]" list which is not a valid list of patheffects. In fact this rcParam cannot be meaningfully set from matplotlibrc; one would need to do an eval() like for axes.prop_cycle but let's not get there... I changed the validator to validate_anylist, which at least works for the default of an empty list... - we need to be a bit more careful when constructing the global rcParams instance as well as rcParamsOrig, rcParamsDefault, to not resolve _auto_backend_sentinel too early. One can check that the rcParams are unchanged by printing them out -- that catches a typo: two entries in font.fantasy should be "Impact", "Western"; not "ImpactWestern".
1 parent 764cc4a commit a6a838d

File tree

6 files changed

+1136
-1209
lines changed

6 files changed

+1136
-1209
lines changed

.flake8

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ per-file-ignores =
3535
lib/matplotlib/_mathtext_data.py: E203, E261
3636
lib/matplotlib/font_manager.py: E203, E221, E251, E501
3737
lib/matplotlib/mathtext.py: E221, E251
38-
lib/matplotlib/rcsetup.py: E501
3938
lib/matplotlib/tests/test_mathtext.py: E501
4039
lib/matplotlib/transforms.py: E201, E202, E203
4140
lib/matplotlib/tri/triinterpolate.py: E201, E221

lib/matplotlib/__init__.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
from matplotlib.cbook import (
140140
MatplotlibDeprecationWarning, dedent, get_label, sanitize_sequence)
141141
from matplotlib.cbook import mplDeprecation # deprecated
142-
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
142+
from matplotlib.rcsetup import validate_backend, cycler
143143

144144
import numpy
145145

@@ -614,7 +614,8 @@ def get_cachedir():
614614
return _get_config_or_cache_dir(_get_xdg_cache_dir())
615615

616616

617-
def _get_data_path():
617+
@_logged_cached('matplotlib data path: %s')
618+
def get_data_path():
618619
"""Return the path to matplotlib data."""
619620

620621
if 'MATPLOTLIBDATA' in os.environ:
@@ -655,13 +656,6 @@ def get_candidate_paths():
655656
raise RuntimeError('Could not find the matplotlib data files')
656657

657658

658-
@_logged_cached('matplotlib data path: %s')
659-
def get_data_path():
660-
if defaultParams['datapath'][0] is None:
661-
defaultParams['datapath'][0] = _get_data_path()
662-
return defaultParams['datapath'][0]
663-
664-
665659
@cbook.deprecated("3.1")
666660
def get_py2exe_datafiles():
667661
data_path = Path(get_data_path())
@@ -749,9 +743,7 @@ class RcParams(MutableMapping, dict):
749743
:ref:`customizing-with-matplotlibrc-files`
750744
"""
751745

752-
validate = {key: converter
753-
for key, (default, converter) in defaultParams.items()
754-
if key not in _all_deprecated}
746+
validate = rcsetup._validators
755747

756748
# validate values on the way in
757749
def __init__(self, *args, **kwargs):
@@ -807,6 +799,9 @@ def __getitem__(self, key):
807799
from matplotlib import pyplot as plt
808800
plt.switch_backend(rcsetup._auto_backend_sentinel)
809801

802+
elif key == "datapath":
803+
return get_data_path()
804+
810805
return dict.__getitem__(self, key)
811806

812807
def __repr__(self):
@@ -916,7 +911,7 @@ def _rc_params_in_file(fname, fail_on_error=False):
916911
config = RcParams()
917912

918913
for key, (val, line, line_no) in rc_temp.items():
919-
if key in defaultParams:
914+
if key in rcsetup._validators:
920915
if fail_on_error:
921916
config[key] = val # try to convert to proper type or raise
922917
else:
@@ -962,11 +957,8 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
962957
if not use_default_template:
963958
return config_from_file
964959

965-
iter_params = defaultParams.items()
966960
with cbook._suppress_matplotlib_deprecation_warning():
967-
config = RcParams([(key, default) for key, (default, _) in iter_params
968-
if key not in _all_deprecated])
969-
config.update(config_from_file)
961+
config = RcParams({**rcParamsDefault, **config_from_file})
970962

971963
if config['datapath'] is None:
972964
config['datapath'] = get_data_path()
@@ -984,15 +976,28 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
984976
return config
985977

986978

987-
# this is the instance used by the matplotlib classes
988-
rcParams = rc_params()
979+
# When constructing the global instances, we need to perform certain updates
980+
# by explicitly calling the superclass (dict.update, dict.items) to avoid
981+
# triggering resolution of _auto_backend_sentinel.
989982

983+
rcParamsDefault = _rc_params_in_file(
984+
cbook._get_data_path("matplotlibrc"), fail_on_error=True)
985+
with cbook._suppress_matplotlib_deprecation_warning():
986+
dict.update(rcParamsDefault, rcsetup._hardcoded_defaults)
987+
988+
rcParams = RcParams() # The global instance.
989+
with cbook._suppress_matplotlib_deprecation_warning():
990+
dict.update(rcParams, dict.items(rcParamsDefault))
991+
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
990992

991993
with cbook._suppress_matplotlib_deprecation_warning():
994+
defaultParams = rcsetup.defaultParams = { # Left only for backcompat.
995+
# We want to resolve deprecated rcParams, but not backend...
996+
key: [(rcsetup._auto_backend_sentinel if key == "backend" else
997+
rcParamsDefault[key]),
998+
validator]
999+
for key, validator in rcsetup._validators.items()}
9921000
rcParamsOrig = RcParams(rcParams.copy())
993-
rcParamsDefault = RcParams([(key, default) for key, (default, converter) in
994-
defaultParams.items()
995-
if key not in _all_deprecated])
9961001

9971002
if rcParams['axes.formatter.use_locale']:
9981003
locale.setlocale(locale.LC_ALL, '')

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def get_sample_data(fname, asfileobj=True):
439439
440440
If the filename ends in .gz, the file is implicitly ungzipped.
441441
"""
442-
path = Path(matplotlib._get_data_path(), 'sample_data', fname)
442+
path = _get_data_path('sample_data', fname)
443443
if asfileobj:
444444
suffix = path.suffix.lower()
445445
if suffix == '.gz':

0 commit comments

Comments
 (0)