23
23
BASE_LIBRARY_PATH = os .path .join (_here , 'stylelib' )
24
24
# Users may want multiple library paths, so store a list of paths.
25
25
USER_LIBRARY_PATHS = [os .path .join ('~' , '.matplotlib' , 'stylelib' )]
26
- STYLE_FILE_PATTERN = re .compile ('([A-Za-z._-]+).style$' )
26
+ STYLE_FILE_PATTERN = re .compile ('([\S]+).style$' )
27
+
28
+
29
+ def is_style_file (filename ):
30
+ """Return True if the filename looks like a style file."""
31
+ return STYLE_FILE_PATTERN .match (filename ) is not None
27
32
28
33
29
34
def use (name ):
@@ -32,13 +37,23 @@ def use(name):
32
37
Parameters
33
38
----------
34
39
name : str or list of str
35
- Name of style. For list of available styles see `style.available`.
36
- If given a list, each style is applied from first to last in the list.
40
+ Name of style or path/URL to a style file. For a list of available
41
+ style names, see `style.available`. If given a list, each style is
42
+ applied from first to last in the list.
37
43
"""
38
44
if np .isscalar (name ):
39
45
name = [name ]
40
- for s in name :
41
- plt .rcParams .update (library [s ])
46
+
47
+ for style in name :
48
+ if is_style_file (style ):
49
+ settings = mpl .rc_params_in_file (style )
50
+ plt .rcParams .update (settings )
51
+ elif style not in library :
52
+ msg = ("'%s' not found in the style library. "
53
+ "See `style.available` for list of available styles." )
54
+ raise ValueError (msg % style )
55
+ else :
56
+ plt .rcParams .update (library [style ])
42
57
43
58
44
59
def load_base_library ():
@@ -67,8 +82,8 @@ def iter_style_files(style_dir):
67
82
"""Yield file path and name of styles in the given directory."""
68
83
for path in os .listdir (style_dir ):
69
84
filename = os .path .basename (path )
70
- match = STYLE_FILE_PATTERN . match (filename )
71
- if match :
85
+ if is_style_file (filename ):
86
+ match = STYLE_FILE_PATTERN . match ( filename )
72
87
path = os .path .abspath (os .path .join (style_dir , path ))
73
88
yield path , match .groups ()[0 ]
74
89
@@ -91,8 +106,6 @@ def update_nested_dict(main_dict, new_dict):
91
106
# update named styles specified by user
92
107
for name , rc_dict in new_dict .iteritems ():
93
108
if name in main_dict :
94
- # FIXME: This is currently broken because rc_params_from_file fills
95
- # in all settings so the update overwrites all values.
96
109
main_dict [name ].update (rc_dict )
97
110
else :
98
111
main_dict [name ] = rc_dict
0 commit comments