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
@@ -45,23 +45,6 @@ class __getattr__:
45
45
'docstring.hardcopy' , 'date.epoch' }
46
46
47
47
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
-
65
48
@_docstring .Substitution (
66
49
"\n " .join (map ("- {}" .format , sorted (STYLE_BLACKLIST , key = str .lower )))
67
50
)
@@ -110,10 +93,10 @@ def use(style):
110
93
111
94
style_alias = {'mpl20' : 'default' , 'mpl15' : 'classic' }
112
95
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 [
117
100
"seaborn" ,
118
101
"seaborn-bright" ,
119
102
"seaborn-colorblind" ,
@@ -137,28 +120,33 @@ def fix_style(s):
137
120
"correspond to the styles shipped by seaborn. However, "
138
121
"they will remain available as 'seaborn-v0_8-<style>'. "
139
122
"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 )):
154
134
try :
155
- rc = rc_params_from_file (style , use_default_template = False )
156
- _apply_style (rc )
135
+ style = _rc_params_in_file (style )
157
136
except IOError as err :
158
137
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 )
162
150
163
151
164
152
@contextlib .contextmanager
@@ -223,8 +211,7 @@ def read_style_directory(style_dir):
223
211
styles = dict ()
224
212
for path in Path (style_dir ).glob (f"*.{ STYLE_EXTENSION } " ):
225
213
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 )
228
215
for w in warns :
229
216
_log .warning ('In %s: %s' , path , w .message )
230
217
return styles
0 commit comments