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

Skip to content

Commit 31d4f24

Browse files
committed
Inline style application logic into mpl.style.use.
... and use the shorter _rc_params_in_file.
1 parent 3811658 commit 31d4f24

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

lib/matplotlib/style/core.py

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import warnings
2020

2121
import matplotlib as mpl
22-
from matplotlib import _api, _docstring, rc_params_from_file, rcParamsDefault
22+
from matplotlib import _api, _docstring, _rc_params_in_file, rcParamsDefault
2323

2424
_log = logging.getLogger(__name__)
2525

@@ -71,23 +71,6 @@ class __getattr__:
7171
"directly use the seaborn API instead.")
7272

7373

74-
def _remove_blacklisted_style_params(d, warn=True):
75-
o = {}
76-
for key in d: # prevent triggering RcParams.__getitem__('backend')
77-
if key in STYLE_BLACKLIST:
78-
if warn:
79-
_api.warn_external(
80-
f"Style includes a parameter, {key!r}, that is not "
81-
"related to style. Ignoring this parameter.")
82-
else:
83-
o[key] = d[key]
84-
return o
85-
86-
87-
def _apply_style(d, warn=True):
88-
mpl.rcParams.update(_remove_blacklisted_style_params(d, warn=warn))
89-
90-
9174
@_docstring.Substitution(
9275
"\n".join(map("- {}".format, sorted(STYLE_BLACKLIST, key=str.lower)))
9376
)
@@ -136,33 +119,38 @@ def use(style):
136119

137120
style_alias = {'mpl20': 'default', 'mpl15': 'classic'}
138121

139-
def fix_style(s):
140-
if isinstance(s, str):
141-
s = style_alias.get(s, s)
142-
if s in _DEPRECATED_SEABORN_STYLES:
122+
for style in styles:
123+
if isinstance(style, str):
124+
style = style_alias.get(style, style)
125+
if style in _DEPRECATED_SEABORN_STYLES:
143126
_api.warn_deprecated("3.6", message=_DEPRECATED_SEABORN_MSG)
144-
s = _DEPRECATED_SEABORN_STYLES[s]
145-
return s
146-
147-
for style in map(fix_style, styles):
148-
if not isinstance(style, (str, Path)):
149-
_apply_style(style)
150-
elif style == 'default':
151-
# Deprecation warnings were already handled when creating
152-
# rcParamsDefault, no need to reemit them here.
153-
with _api.suppress_matplotlib_deprecation_warning():
154-
_apply_style(rcParamsDefault, warn=False)
155-
elif style in library:
156-
_apply_style(library[style])
157-
else:
127+
style = _DEPRECATED_SEABORN_STYLES[style]
128+
if style == "default":
129+
# Deprecation warnings were already handled when creating
130+
# rcParamsDefault, no need to reemit them here.
131+
with _api.suppress_matplotlib_deprecation_warning():
132+
# don't trigger RcParams.__getitem__('backend')
133+
style = {k: rcParamsDefault[k] for k in rcParamsDefault
134+
if k not in STYLE_BLACKLIST}
135+
elif style in library:
136+
style = library[style]
137+
if isinstance(style, (str, Path)):
158138
try:
159-
rc = rc_params_from_file(style, use_default_template=False)
160-
_apply_style(rc)
139+
style = _rc_params_in_file(style)
161140
except IOError as err:
162141
raise IOError(
163-
"{!r} not found in the style library and input is not a "
164-
"valid URL or path; see `style.available` for list of "
165-
"available styles".format(style)) from err
142+
f"{style!r} not found in the style library and input is "
143+
f"not a valid URL or path; see `style.available` for the "
144+
f"list of available styles") from err
145+
filtered = {}
146+
for k in style: # don't trigger RcParams.__getitem__('backend')
147+
if k in STYLE_BLACKLIST:
148+
_api.warn_external(
149+
f"Style includes a parameter, {k!r}, that is not "
150+
f"related to style. Ignoring this parameter.")
151+
else:
152+
filtered[k] = style[k]
153+
mpl.rcParams.update(filtered)
166154

167155

168156
@contextlib.contextmanager
@@ -227,8 +215,7 @@ def read_style_directory(style_dir):
227215
styles = dict()
228216
for path in Path(style_dir).glob(f"*.{STYLE_EXTENSION}"):
229217
with warnings.catch_warnings(record=True) as warns:
230-
styles[path.stem] = rc_params_from_file(
231-
path, use_default_template=False)
218+
styles[path.stem] = _rc_params_in_file(path)
232219
for w in warns:
233220
_log.warning('In %s: %s', path, w.message)
234221
return styles

0 commit comments

Comments
 (0)