|
18 | 18 | import os |
19 | 19 | import re |
20 | 20 | import contextlib |
| 21 | +import warnings |
21 | 22 |
|
22 | 23 | import matplotlib as mpl |
23 | 24 | from matplotlib import cbook |
24 | | -from matplotlib import rc_params_from_file |
| 25 | +from matplotlib import rc_params_from_file, rcParamsDefault |
25 | 26 |
|
26 | 27 |
|
27 | 28 | __all__ = ['use', 'context', 'available', 'library', 'reload_library'] |
|
34 | 35 | STYLE_FILE_PATTERN = re.compile('([\S]+).%s$' % STYLE_EXTENSION) |
35 | 36 |
|
36 | 37 |
|
| 38 | +# A list of rcParams that should not be applied from styles |
| 39 | +STYLE_BLACKLIST = set([ |
| 40 | + 'interactive', 'backend', 'backend.qt4', 'webagg.port', |
| 41 | + 'webagg.port_retries', 'webagg.open_in_browser', 'backend_fallback', |
| 42 | + 'toolbar', 'timezone', 'datapath', 'figure.max_open_warning', |
| 43 | + 'savefig.directory', 'tk.window_focus', 'hardcopy.docstring']) |
| 44 | + |
| 45 | + |
| 46 | +def _remove_blacklisted_style_params(d, warn=True): |
| 47 | + o = {} |
| 48 | + for key, val in d.items(): |
| 49 | + if key in STYLE_BLACKLIST: |
| 50 | + if warn: |
| 51 | + warnings.warn( |
| 52 | + "Style includes a parameter, '{0}', that is not related " |
| 53 | + "to style. Ignoring".format(key)) |
| 54 | + else: |
| 55 | + o[key] = val |
| 56 | + return o |
| 57 | + |
| 58 | + |
37 | 59 | def is_style_file(filename): |
38 | 60 | """Return True if the filename looks like a style file.""" |
39 | 61 | return STYLE_FILE_PATTERN.match(filename) is not None |
40 | 62 |
|
41 | 63 |
|
| 64 | +def _apply_style(d, warn=True): |
| 65 | + mpl.rcParams.update(_remove_blacklisted_style_params(d, warn=warn)) |
| 66 | + |
| 67 | + |
42 | 68 | def use(style): |
43 | 69 | """Use matplotlib style settings from a style specification. |
44 | 70 |
|
@@ -71,18 +97,15 @@ def use(style): |
71 | 97 |
|
72 | 98 | for style in styles: |
73 | 99 | if not cbook.is_string_like(style): |
74 | | - mpl.rcParams.update(style) |
75 | | - continue |
| 100 | + _apply_style(style) |
76 | 101 | elif style == 'default': |
77 | | - mpl.rcdefaults() |
78 | | - continue |
79 | | - |
80 | | - if style in library: |
81 | | - mpl.rcParams.update(library[style]) |
| 102 | + _apply_style(rcParamsDefault, warn=False) |
| 103 | + elif style in library: |
| 104 | + _apply_style(library[style]) |
82 | 105 | else: |
83 | 106 | try: |
84 | 107 | rc = rc_params_from_file(style, use_default_template=False) |
85 | | - mpl.rcParams.update(rc) |
| 108 | + _apply_style(rc) |
86 | 109 | except IOError: |
87 | 110 | msg = ("'%s' not found in the style library and input is " |
88 | 111 | "not a valid URL or path. See `style.available` for " |
|
0 commit comments