def inject_viscid_styles(show_warning=True): try: from matplotlib import rc_params_from_file, style styl_dir = os.path.realpath(os.path.dirname(__file__)) styl_dir = os.path.abspath(os.path.join(styl_dir, "styles")) style_sheets = glob(os.path.join(styl_dir, "*.mplstyle")) if LooseVersion(matplotlib.__version__) < LooseVersion("1.5.0"): tmpfname = tempfile.mkstemp()[1] else: tmpfname = None for styl_fname in style_sheets: styl_name = os.path.splitext(os.path.basename(styl_fname))[0] if tmpfname: # hack the cycler stuff back to the pre-1.5.0 syntax with open(styl_fname, 'r') as fin: with open(tmpfname, 'w') as fout: fout.write(_cycler2prop_cycle(fin.read())) styl_fname = tmpfname params = rc_params_from_file(styl_fname, use_default_template=False) style.library[styl_name] = params if tmpfname: os.unlink(tmpfname) style.reload_library() except ImportError: if show_warning: logger.debug("Upgrade to matplotlib >= 1.5.0 to use style sheets")
def temp_style(style_name, settings=None): """Context manager to create a style sheet in a temporary directory.""" if not settings: settings = DUMMY_SETTINGS temp_file = '%s.%s' % (style_name, STYLE_EXTENSION) try: with TemporaryDirectory() as tmpdir: # Write style settings to file in the tmpdir. Path(tmpdir, temp_file).write_text( "\n".join("{}: {}".format(k, v) for k, v in settings.items())) # Add tmpdir to style path and reload so we can access this style. USER_LIBRARY_PATHS.append(tmpdir) style.reload_library() yield finally: style.reload_library()
def temp_style(style_name, settings=None): """Context manager to create a style sheet in a temporary directory.""" settings = DUMMY_SETTINGS temp_file = '%s.%s' % (style_name, STYLE_EXTENSION) # Write style settings to file in the temp directory. tempdir = tempfile.mkdtemp() with open(os.path.join(tempdir, temp_file), 'w') as f: for k, v in six.iteritems(settings): f.write('%s: %s' % (k, v)) # Add temp directory to style path and reload so we can access this style. USER_LIBRARY_PATHS.append(tempdir) style.reload_library() try: yield finally: shutil.rmtree(tempdir) style.reload_library()