如何运送或分发matplotlib样式表

问题描述 投票:5回答:1

我想发布一个自定义matplotlib style sheet,但现在我能想到的唯一的办法就是上传到要点或其他一些网站,并告诉我的用户手动下载到一些配置目录。

有没有一种方法,就好像它是一个Python包,或作为一个模块的一部分发布一个样式表?一些容易喜欢pip install mpl_fancy

python matplotlib packaging
1个回答
1
投票

阅读从@aloctavodia链接和咨询massmutual/mmviz-python GitHub repo之后,这是我想出了。

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,
    }
)

my_style.py我只是把一个基本的例子脚本。现在,我的用户可以安装这种风格与pip install git+https://github.com/me/my-style

© www.soinside.com 2019 - 2024. All rights reserved.