@@ -39,53 +39,88 @@ def is_style_file(filename):
39
39
return STYLE_FILE_PATTERN .match (filename ) is not None
40
40
41
41
42
- def use (name ):
43
- """Use matplotlib style settings from a known style sheet or from a file .
42
+ def use (style ):
43
+ """Use matplotlib style settings from a style specification .
44
44
45
45
Parameters
46
46
----------
47
- name : str or list of str
48
- Name of style or path/URL to a style file. For a list of available
49
- style names, see `style.available`. If given a list, each style is
50
- applied from first to last in the list.
47
+ style : str, dict, or list
48
+ A style specification. Valid options are:
49
+
50
+ +------+-------------------------------------------------------------+
51
+ | str | The name of a style or a path/URL to a style file. For a |
52
+ | | list of available style names, see `style.available`. |
53
+ +------+-------------------------------------------------------------+
54
+ | dict | Dictionary with valid key/value pairs for |
55
+ | | `matplotlib.rcParams`. |
56
+ +------+-------------------------------------------------------------+
57
+ | list | A list of style specifiers (str or dict) applied from first |
58
+ | | to last in the list. |
59
+ +------+-------------------------------------------------------------+
60
+
61
+
51
62
"""
52
- if cbook .is_string_like (name ):
53
- name = [name ]
63
+ if cbook .is_string_like (style ) or hasattr (style , 'keys' ):
64
+ # If name is a single str or dict, make it a single element list.
65
+ styles = [style ]
66
+ else :
67
+ styles = style
68
+
69
+ for style in styles :
70
+ if not cbook .is_string_like (style ):
71
+ mpl .rcParams .update (style )
72
+ continue
54
73
55
- for style in name :
56
74
if style in library :
57
75
mpl .rcParams .update (library [style ])
58
76
else :
59
77
try :
60
78
rc = rc_params_from_file (style , use_default_template = False )
61
79
mpl .rcParams .update (rc )
62
- except :
80
+ except IOError :
63
81
msg = ("'%s' not found in the style library and input is "
64
82
"not a valid URL or path. See `style.available` for "
65
83
"list of available styles." )
66
- raise ValueError (msg % style )
84
+ raise IOError (msg % style )
67
85
68
86
69
87
@contextlib .contextmanager
70
- def context (name , after_reset = False ):
88
+ def context (style , after_reset = False ):
71
89
"""Context manager for using style settings temporarily.
72
90
73
91
Parameters
74
92
----------
75
- name : str or list of str
76
- Name of style or path/URL to a style file. For a list of available
77
- style names, see `style.available`. If given a list, each style is
78
- applied from first to last in the list.
93
+ style : str, dict, or list
94
+ A style specification. Valid options are:
95
+
96
+ +------+-------------------------------------------------------------+
97
+ | str | The name of a style or a path/URL to a style file. For a |
98
+ | | list of available style names, see `style.available`. |
99
+ +------+-------------------------------------------------------------+
100
+ | dict | Dictionary with valid key/value pairs for |
101
+ | | `matplotlib.rcParams`. |
102
+ +------+-------------------------------------------------------------+
103
+ | list | A list of style specifiers (str or dict) applied from first |
104
+ | | to last in the list. |
105
+ +------+-------------------------------------------------------------+
106
+
79
107
after_reset : bool
80
108
If True, apply style after resetting settings to their defaults;
81
109
otherwise, apply style on top of the current settings.
82
110
"""
83
111
initial_settings = mpl .rcParams .copy ()
84
112
if after_reset :
85
113
mpl .rcdefaults ()
86
- use (name )
87
- yield
88
- mpl .rcParams .update (initial_settings )
114
+ try :
115
+ use (style )
116
+ except :
117
+ # Restore original settings before raising errors during the update.
118
+ mpl .rcParams .update (initial_settings )
119
+ raise
120
+ else :
121
+ yield
122
+ finally :
123
+ mpl .rcParams .update (initial_settings )
89
124
90
125
91
126
def load_base_library ():
0 commit comments