【问题标题】:How can I share matplotlib style?如何分享 matplotlib 风格?
【发布时间】:2016-06-21 10:38:53
【问题描述】:

可以在matplotlib 中加载自定义绘图样式,例如:

>>> import matplotlib.pyplot as plt
>>> plt.style.use('ggplot')

而且我知道我可以创建自己的,http://matplotlib.org/users/style_sheets.html 解释了如何。

假设我创建了一个令人惊叹的 matplotlib 样式——我如何与其他人分享它?有没有办法用 pip/conda 或其他合适的方法来做到这一点?

文档包含“创建自定义样式并通过调用 style.use 以及样式表的路径或 URL 来使用它们”的建议。 - 所以我想我可以在一些公共 git 存储库上维护一个链接,如果他们放那个 URL,人们就会得到最新的样式?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以将代码组织成这样的结构:

    |
    └─── setup.py
    └─── mplstyles
             style_01.mplstyle
             style_02.mplstyle
    

    然后,在您的文件 setup.py 中写入如下内容:

    # -*- coding: utf-8 -*-
    import matplotlib as mpl
    import glob
    import os.path
    import shutil
    import argparse
    
    parser = argparse.ArgumentParser()
    parser.add_argument('-install', action='store_true', default=True)
    parser.add_argument('-upgrade', action='store_true')
    options = parser.parse_args()
    
    #~ # ref  ->  matplotlib/style/core
    BASE_LIBRARY_PATH = os.path.join(mpl.get_data_path(), 'stylelib')
    STYLE_PATH = os.path.join(os.getcwd(),'mplstyles')
    STYLE_EXTENSION = 'mplstyle'
    style_files = glob.glob(os.path.join(STYLE_PATH,"*.%s"%(STYLE_EXTENSION)))
    
    for _path_file in style_files:
        _, fname = os.path.split(_path_file)
        dest = os.path.join(BASE_LIBRARY_PATH, fname)
        if not os.path.isfile(dest) and options.install:
            shutil.copy(_path_file, dest)
            print("%s style installed"%(fname))
        elif options.upgrade:
            shutil.copy(_path_file, dest)
            print("%s style upgraded"%(fname))
        elif os.path.isfile(dest):
            print("%s style already exists (use -upgrade to upgrade)"%(fname))
        else:
            pass # ¿?
    

    上面的代码将每个 .mplstyle(或样式表)文件从“mplstyles”文件夹复制到 Matplotlib 安装目录。

    “安装”样式

    >> python setup.py -install
    

    “升级”样式

    >> python setup.py -upgrade
    

    【讨论】:

      【解决方案2】:

      我刚刚也有同样的问题。一个尚未解决的小问题。我找到了一个能够使用 PyPi 分发样式的解决方案(在我的情况下是 goosempl,也在 GitHub 上)。

      我创建了一个 Python 模块,mplstyle-files 是其中的一部分:

      |-- setup.py
      |-- package_name
      |   |-- __init__.py
      |   |-- styles
      |   |   |-- example.mplstyle
      

      现在的想法是:

      1. .mplstyle 文件与模块打包在一起。
      2. 模块已安装。
      3. 安装结束时会运行一个小脚本,从新安装的包中提取 .mplstyle 文件并将它们写入 matplotlib 配置目录。

      这里是要点

      setup.py

      ​​>
      import atexit
      from setuptools                 import setup
      from setuptools.command.install import install
      
      def _post_install():
          import goosempl
          package_name.copy_style()
      
      class new_install(install):
          def __init__(self, *args, **kwargs):
              super(new_install, self).__init__(*args, **kwargs)
              atexit.register(_post_install)
      
      __version__ = '0.1.0'
      
      setup(
          name              = 'package_name',
          version           = __version__,
          ...
          install_requires  = ['matplotlib>=2.0.0'],
          packages          = ['package_name'],
          cmdclass          = {'install': new_install},
          package_data      = {'package_name/styles':[
              'package_name/styles/example.mplstyle',
          ]},
      )
      

      初始化.py

      ​​>
      def copy_style():
      
        import os
        import matplotlib
      
        from pkg_resources import resource_string
      
        files = [
          'styles/example.mplstyle',
        ]
      
        for fname in files:
          path = os.path.join(matplotlib.get_configdir(),fname)
          text = resource_string(__name__,fname).decode()
          open(path,'w').write(text)
      

      为了将来参考,请参阅这些相关问题/文档:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-25
        • 2010-11-01
        • 2014-01-05
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 1970-01-01
        • 2011-03-08
        相关资源
        最近更新 更多