@@ -39,53 +39,88 @@ def is_style_file(filename):
3939 return STYLE_FILE_PATTERN .match (filename ) is not None
4040
4141
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 .
4444
4545 Parameters
4646 ----------
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+
5162 """
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
5473
55- for style in name :
5674 if style in library :
5775 mpl .rcParams .update (library [style ])
5876 else :
5977 try :
6078 rc = rc_params_from_file (style , use_default_template = False )
6179 mpl .rcParams .update (rc )
62- except :
80+ except IOError :
6381 msg = ("'%s' not found in the style library and input is "
6482 "not a valid URL or path. See `style.available` for "
6583 "list of available styles." )
66- raise ValueError (msg % style )
84+ raise IOError (msg % style )
6785
6886
6987@contextlib .contextmanager
70- def context (name , after_reset = False ):
88+ def context (style , after_reset = False ):
7189 """Context manager for using style settings temporarily.
7290
7391 Parameters
7492 ----------
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+
79107 after_reset : bool
80108 If True, apply style after resetting settings to their defaults;
81109 otherwise, apply style on top of the current settings.
82110 """
83111 initial_settings = mpl .rcParams .copy ()
84112 if after_reset :
85113 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 )
89124
90125
91126def load_base_library ():
0 commit comments