|
18 | 18 | import warnings
|
19 | 19 |
|
20 | 20 | 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 |
22 | 22 |
|
23 | 23 | _log = logging.getLogger(__name__)
|
24 | 24 |
|
|
64 | 64 | "directly use the seaborn API instead.")
|
65 | 65 |
|
66 | 66 |
|
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 |
| - |
84 | 67 | @_docstring.Substitution(
|
85 | 68 | "\n".join(map("- {}".format, sorted(STYLE_BLACKLIST, key=str.lower)))
|
86 | 69 | )
|
@@ -129,33 +112,38 @@ def use(style):
|
129 | 112 |
|
130 | 113 | style_alias = {'mpl20': 'default', 'mpl15': 'classic'}
|
131 | 114 |
|
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: |
136 | 119 | _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)): |
151 | 131 | try:
|
152 |
| - rc = rc_params_from_file(style, use_default_template=False) |
153 |
| - _apply_style(rc) |
| 132 | + style = _rc_params_in_file(style) |
154 | 133 | except IOError as err:
|
155 | 134 | 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) |
159 | 147 |
|
160 | 148 |
|
161 | 149 | @contextlib.contextmanager
|
@@ -205,8 +193,7 @@ def read_style_directory(style_dir):
|
205 | 193 | styles = dict()
|
206 | 194 | for path in Path(style_dir).glob(f"*.{STYLE_EXTENSION}"):
|
207 | 195 | 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) |
210 | 197 | for w in warns:
|
211 | 198 | _log.warning('In %s: %s', path, w.message)
|
212 | 199 | return styles
|
|
0 commit comments