|
| 1 | +""" |
| 2 | +Core functions and attributes for the matplotlib style library: |
| 3 | +
|
| 4 | +``use`` |
| 5 | + Select style sheet to override the current matplotlib settings. |
| 6 | +``available`` |
| 7 | + List available style sheets. |
| 8 | +``library`` |
| 9 | + A dictionary of style names and matplotlib settings. |
| 10 | +""" |
| 11 | +import os |
| 12 | +import re |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import matplotlib.pyplot as plt |
| 16 | +import matplotlib as mpl |
| 17 | + |
| 18 | + |
| 19 | +__all__ = ['use', 'available', 'library'] |
| 20 | + |
| 21 | + |
| 22 | +_here = os.path.abspath(os.path.dirname(__file__)) |
| 23 | +BASE_LIBRARY_PATH = os.path.join(_here, 'stylelib') |
| 24 | +# Users may want multiple library paths, so store a list of paths. |
| 25 | +USER_LIBRARY_PATHS = [os.path.join('~', '.matplotlib', 'stylelib')] |
| 26 | +STYLE_FILE_PATTERN = re.compile('([A-Za-z._-]+).mplrc$') |
| 27 | + |
| 28 | + |
| 29 | +def use(name): |
| 30 | + """Use matplotlib rc parameters from a pre-defined name or from a file. |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + 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. |
| 37 | + """ |
| 38 | + if np.isscalar(name): |
| 39 | + name = [name] |
| 40 | + for s in name: |
| 41 | + plt.rcParams.update(library[s]) |
| 42 | + |
| 43 | + |
| 44 | +def load_base_library(): |
| 45 | + """Load style library defined in this package.""" |
| 46 | + library = dict() |
| 47 | + library.update(read_style_directory(BASE_LIBRARY_PATH)) |
| 48 | + return library |
| 49 | + |
| 50 | + |
| 51 | +def iter_user_libraries(): |
| 52 | + for stylelib_path in USER_LIBRARY_PATHS: |
| 53 | + stylelib_path = os.path.expanduser(stylelib_path) |
| 54 | + if os.path.exists(stylelib_path) and os.path.isdir(stylelib_path): |
| 55 | + yield stylelib_path |
| 56 | + |
| 57 | + |
| 58 | +def update_user_library(library): |
| 59 | + """Update style library with user-defined rc files""" |
| 60 | + for stylelib_path in iter_user_libraries(): |
| 61 | + styles = read_style_directory(stylelib_path) |
| 62 | + update_nested_dict(library, styles) |
| 63 | + return library |
| 64 | + |
| 65 | + |
| 66 | +def iter_style_files(style_dir): |
| 67 | + """Yield file path and name of styles in the given directory.""" |
| 68 | + for path in os.listdir(style_dir): |
| 69 | + filename = os.path.basename(path) |
| 70 | + match = STYLE_FILE_PATTERN.match(filename) |
| 71 | + if match: |
| 72 | + path = os.path.abspath(os.path.join(style_dir, path)) |
| 73 | + yield path, match.groups()[0] |
| 74 | + |
| 75 | + |
| 76 | +def read_style_directory(style_dir): |
| 77 | + """Return dictionary of styles defined in `style_dir`.""" |
| 78 | + styles = dict() |
| 79 | + for path, name in iter_style_files(style_dir): |
| 80 | + styles[name] = mpl.rc_params_from_file(path) |
| 81 | + return styles |
| 82 | + |
| 83 | + |
| 84 | +def update_nested_dict(main_dict, new_dict): |
| 85 | + """Update nested dict (only level of nesting) with new values. |
| 86 | +
|
| 87 | + Unlike dict.update, this assumes that the values of the parent dict are |
| 88 | + dicts (or dict-like), so you shouldn't replace the nested dict if it |
| 89 | + already exists. Instead you should update the sub-dict. |
| 90 | + """ |
| 91 | + # update named styles specified by user |
| 92 | + for name, rc_dict in new_dict.iteritems(): |
| 93 | + 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 | + main_dict[name].update(rc_dict) |
| 97 | + else: |
| 98 | + main_dict[name] = rc_dict |
| 99 | + return main_dict |
| 100 | + |
| 101 | + |
| 102 | +# Load style library |
| 103 | +# ================== |
| 104 | +_base_library = load_base_library() |
| 105 | +library = update_user_library(_base_library) |
| 106 | +available = library.keys() |
0 commit comments