|
19 | 19 | import warnings
|
20 | 20 |
|
21 | 21 | 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 |
23 | 23 |
|
24 | 24 | _log = logging.getLogger(__name__)
|
25 | 25 |
|
@@ -71,23 +71,6 @@ class __getattr__:
|
71 | 71 | "directly use the seaborn API instead.")
|
72 | 72 |
|
73 | 73 |
|
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 |
| - |
91 | 74 | @_docstring.Substitution(
|
92 | 75 | "\n".join(map("- {}".format, sorted(STYLE_BLACKLIST, key=str.lower)))
|
93 | 76 | )
|
@@ -136,33 +119,38 @@ def use(style):
|
136 | 119 |
|
137 | 120 | style_alias = {'mpl20': 'default', 'mpl15': 'classic'}
|
138 | 121 |
|
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: |
143 | 126 | _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)): |
158 | 138 | try:
|
159 |
| - rc = rc_params_from_file(style, use_default_template=False) |
160 |
| - _apply_style(rc) |
| 139 | + style = _rc_params_in_file(style) |
161 | 140 | except IOError as err:
|
162 | 141 | 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) |
166 | 154 |
|
167 | 155 |
|
168 | 156 | @contextlib.contextmanager
|
@@ -227,8 +215,7 @@ def read_style_directory(style_dir):
|
227 | 215 | styles = dict()
|
228 | 216 | for path in Path(style_dir).glob(f"*.{STYLE_EXTENSION}"):
|
229 | 217 | 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) |
232 | 219 | for w in warns:
|
233 | 220 | _log.warning('In %s: %s', path, w.message)
|
234 | 221 | return styles
|
|
0 commit comments