Skip to content

Commit c54afff

Browse files
committed
Inline style application logic into mpl.style.use.
... and use the shorter _rc_params_in_file.
1 parent 705d250 commit c54afff

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

lib/matplotlib/style/core.py

+30-43
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import warnings
1919

2020
import matplotlib as mpl
21-
from matplotlib import _api, _docstring, rc_params_from_file, rcParamsDefault
21+
from matplotlib import _api, _docstring, _rc_params_in_file, rcParamsDefault
2222

2323
_log = logging.getLogger(__name__)
2424

@@ -64,23 +64,6 @@
6464
"directly use the seaborn API instead.")
6565

6666

67-
def _remove_blacklisted_style_params(d, warn=True):
68-
o = {}
69-
for key in d: # prevent triggering RcParams.__getitem__('backend')
70-
if key in STYLE_BLACKLIST:
71-
if warn:
72-
_api.warn_external(
73-
f"Style includes a parameter, {key!r}, that is not "
74-
"related to style. Ignoring this parameter.")
75-
else:
76-
o[key] = d[key]
77-
return o
78-
79-
80-
def _apply_style(d, warn=True):
81-
mpl.rcParams.update(_remove_blacklisted_style_params(d, warn=warn))
82-
83-
8467
@_docstring.Substitution(
8568
"\n".join(map("- {}".format, sorted(STYLE_BLACKLIST, key=str.lower)))
8669
)
@@ -129,33 +112,38 @@ def use(style):
129112

130113
style_alias = {'mpl20': 'default', 'mpl15': 'classic'}
131114

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

160148

161149
@contextlib.contextmanager
@@ -205,8 +193,7 @@ def read_style_directory(style_dir):
205193
styles = dict()
206194
for path in Path(style_dir).glob(f"*.{STYLE_EXTENSION}"):
207195
with warnings.catch_warnings(record=True) as warns:
208-
styles[path.stem] = rc_params_from_file(
209-
path, use_default_template=False)
196+
styles[path.stem] = _rc_params_in_file(path)
210197
for w in warns:
211198
_log.warning('In %s: %s', path, w.message)
212199
return styles

0 commit comments

Comments
 (0)