可以使用以下内容加载自定义绘图样式matplotlib:
>>> import matplotlib.pyplot as plt
>>> plt.style.use('ggplot')
Run Code Online (Sandbox Code Playgroud)
而且我知道我可以创建自己的,http://matplotlib.org/users/style_sheets.html解释了如何.
让我们说我创造了一个惊人的matplotlib风格 - 我怎么能与其他人分享?有没有办法用pip/conda或其他合适的方法做到这一点?
这些文档包括"创建自定义样式并通过调用style.use以及样式表的路径或URL来使用它们"的建议. - 所以我想我可以在一些公共git存储库上维护一个链接,如果他们放了那个URL,人们会得到最新的样式?
小智 7
您可以在以下结构中组织代码:
|
???? setup.py
???? mplstyles
style_01.mplstyle
style_02.mplstyle
Run Code Online (Sandbox Code Playgroud)
然后,在您的文件中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 # ¿?
Run Code Online (Sandbox Code Playgroud)
上面的代码将每个.mplstyle(或样式表)文件从"mplstyles"文件夹复制到Matplotlib安装目录.
>> python setup.py -install
Run Code Online (Sandbox Code Playgroud)
>> python setup.py -upgrade
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1252 次 |
| 最近记录: |