6

I want to distribute a custom matplotlib style sheet, but right now the only way I can think of is uploading it to Gist or some other website and tell my users to manually download it to some configuration directory.

Is there a way to distribute a style sheet as if it were a Python package, or as part of a module? Something easy like pip install mpl_fancy.

astrojuanlu
  • 6,744
  • 8
  • 45
  • 105
  • 1
    Please make this a github issue or ping the mpl-dev mailing list, we should provide an API to register styles and then you could have your package do that as a side-effect of being imported. Currently you can reach into `mpl.style.core`, add a path to `USER_LIBRARY_PATHS` to point to your files and then re-run `mpl.style.core.reload_library()`. – tacaswell Jul 22 '15 at 14:27
  • You can also put in a PR adding your style to the mpl repo and we will ship it for you ;) – tacaswell Jul 22 '15 at 14:27
  • I just filed this issue: https://github.com/matplotlib/matplotlib/issues/4781 And for the PR... I might :) – astrojuanlu Jul 24 '15 at 10:56
  • just in case still useful [this](https://stackoverflow.com/a/36902139/815542) works for me – aloctavodia Apr 25 '18 at 11:45

1 Answers1

3

After reading the link from @aloctavodia and consulting the massmutual/mmviz-python GitHub repo, this is what I came up with.

setup.py

from setuptools import setup
from setuptools.command.install import install
import os
import shutil
import atexit

import matplotlib

def install_mplstyle():
    stylefile = "mystyle.mplstyle"

    mpl_stylelib_dir = os.path.join(matplotlib.get_configdir() ,"stylelib")
    if not os.path.exists(mpl_stylelib_dir):
        os.makedirs(mpl_stylelib_dir)

    print("Installing style into", mpl_stylelib_dir)
    shutil.copy(
        os.path.join(os.path.dirname(__file__), stylefile),
        os.path.join(mpl_stylelib_dir, stylefile))

class PostInstallMoveFile(install):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        atexit.register(install_mplstyle)

setup(
    name='my-style',
    version='0.1.0',
    py_modules=['my_style'],
    install_requires=[
        'matplotlib',
    ],
    cmdclass={
        'install': PostInstallMoveFile,
    }
)

Into my_style.py I just put a basic example script. Now, my users can install this style with pip install git+https://github.com/me/my-style!

Seanny123
  • 8,776
  • 13
  • 68
  • 124