8000 Inline style application logic into mpl.style.use. · matplotlib/matplotlib@ca2db5e · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ca2db5e

Browse files
committed
Inline style application logic into mpl.style.use.
... and use the shorter _rc_params_in_file.
1 parent 534077d commit ca2db5e

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

@@ -45,23 +45,6 @@ class __getattr__:
4545
'docstring.hardcopy', 'date.epoch'}
4646

4747

48-
def _remove_blacklisted_style_params(d, warn=True):
49-
o = {}
50-
for key in d: # prevent triggering RcParams.__getitem__('backend')
51-
if key in STYLE_BLACKLIST:
52-
if warn:
53-
_api.warn_external(
54-
f"Style includes a parameter, {key!r}, that is not "
55-
"related to style. Ignoring this parameter.")
56-
else:
57-
o[key] = d[key]
58-
return o
59-
60-
61-
def _apply_style(d, warn=True):
62-
mpl.rcParams.update(_remove_blacklisted_style_params(d, warn=warn))
63-
64-
6548
@_docstring.Substitution(
6649
"\n".join(map("- {}".format, sorted(STYLE_BLACKLIST, key=str.lower)))
6750
)
@@ -110,10 +93,10 @@ def use(style):
11093

11194
style_alias = {'mpl20': 'default', 'mpl15': 'classic'}
11295

113-
def fix_style(s):
114-
if isinstance(s, str):
115-
s = style_alias.get(s, s)
116-
if s in [
96+
for style in styles:
97+
if isinstance(style, str):
98+
style = style_alias.get(style, style)
99+
if style in [
117100
"seaborn",
118101
"seaborn-bright",
119102
"seaborn-colorblind",
@@ -137,28 +120,33 @@ def fix_style(s):
137120
"correspond to the styles shipped by seaborn. However, "
138121
"they will remain available as 'seaborn-v0_8-<style>'. "
139122
"Alternatively, directly use the seaborn API instead.")
140-
s = s.replace("seaborn", "seaborn-v0_8")
141-
return s
142-
143-
for style in map(fix_style, styles):
144-
if not isinstance(style, (str, Path)):
145-
_apply_style(style)
146-
elif style == 'default':
147-
# Deprecation warnings were already handled when creating
148-
# rcParamsDefault, no need to reemit them here.
149-
with _api.suppress_matplotlib_deprecation_warning():
150-
_apply_style(rcParamsDefault, warn=False)
151-
elif style in library:
152-
_apply_style(library[style])
153-
else:
123+
style = style.replace("seaborn", "seaborn-v0_8")
124+
if style == "default":
125+
# Deprecation warnings were already handled when creating
126+
# rcParamsDefault, no need to reemit them here.
127+
with _api.suppress_matplotlib_deprecation_warning():
128+
# don't trigger RcParams.__getitem__('backend')
129+
style = {k: rcParamsDefault[k] for k in rcParamsDefault
130+
if k not in STYLE_BLACKLIST}
131+
elif style in library:
132+
style = library[style]
133+
if isinstance(style, (str, Path)):
154134
try:
155-
rc = rc_params_from_file(style, use_default_template=False)
156-
_apply_style(rc)
135+
style = _rc_params_in_file(style)
157136
except IOError as err:
158137
raise IOError(
159-
"{!r} not found in the style library and input is not a "
160-
"valid URL or path; see `style.available` for list of "
161-
"available styles".format(style)) from err
138+
f"{style!r} not found in the style library and input is "
139+
f"not a valid URL or path; see `style.available` for the "
140+
f"list of available styles") from err
141+
filtered = {}
142+
for k in style: # don't trigger RcParams.__getitem__('backend')
143+
if k in STYLE_BLACKLIST:
144+
_api.warn_external(
145+
f"Style includes a parameter, {k!r}, that is not "
146+
f"related to style. Ignoring this parameter.")
147+
else:
148+
filtered[k] = style[k]
149+
mpl.rcParams.update(filtered)
162150

163151

164152
@contextlib.contextmanager
@@ -223,8 +211,7 @@ def read_style_directory(style_dir):
223211
styles = dict()
224212
for path in Path(style_dir).glob(f"*.{STYLE_EXTENSION}"):
225213
with warnings.catch_warnings(record=True) as warns:
226-
styles[path.stem] = rc_params_from_file(
227-
path, use_default_template=False)
214+
styles[path.stem] = _rc_params_in_file(path)
228215
for w in warns:
229216
_log.warning('In %s: %s', path, w.message)
230217
return styles

0 commit comments

Comments
 (0)
0