.matplotlibrc和默认选项

发布于 2021-01-29 15:01:02

我正在研究适用于Python的matplotlib库。我开始理解它的一些基本复杂性,因为pylab和pyplot之间的区别,并且我正在尝试复制和修改图库中的一些示例。

我仍然不清楚的一件事是配置文件matplotlibrc的实际作用。

目前,我使用Windows 7下的WinPython 3.3.5.0
64位分发版。.matplotlibrc文件位于WinPython-64bit-3.3.5.0 \ python-3.3.5.amd64 \ lib \
site-packages \ matplotlib \ mpl-data \ matplotlibrc

我想开始更改一些选项作为默认字体,因此我打开了它,发现除一行(后端:TkAgg)外的所有行都已注释。

所以我想问一下matplotlib从哪里获取所有默认值(例如fonts属性)。库中是否有另一个文件,或者它们是否以某种方式在库中“硬编码”?谢谢。

关注者
0
被浏览
60
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    matplotlib\__init__.py站点软件包目录中的文档和代码来看,您可以看到该matplotlibrc文件的搜索路径是:

    Search order:
    
     * current working dir                                                                                                                    
     * environ var MATPLOTLIBRC                                                                                                               
     * HOME/.matplotlib/matplotlibrc                                                                                                          
     * MATPLOTLIBDATA/matplotlibrc
    

    如果在这些路径中找不到文件,则会引发警告:

    warnings.warn('Could not find matplotlibrc; using defaults')
    

    matplotlibrc文件只是对现有默认参数的更新。这些可以使用以下命令找到:

    from matplotlib.rcsetup import defaultParams
    

    (这显然在中matplotlib/rcsetup.py

    在该__init__.py文件中,matplotlib循环浏览此字典并定义将用于所有脚本和代码的默认rc参数:

    rcParamsDefault = RcParams([ (key, default) for key, (default, converter) in \
                        defaultParams.iteritems() ])
    

    因此,如果您想知道默认值,请查看:

    In [4]: import matplotlib
    
    In [5]: matplotlib.rcParamsDefault
    Out[5]: 
    {'agg.path.chunksize': 0,
     'animation.bitrate': -1,
     'animation.codec': 'mpeg4',
     'animation.ffmpeg_args': '',
     'animation.ffmpeg_path': 'ffmpeg',
     'animation.frame_format': 'png',
     'animation.mencoder_args': '',
     'animation.mencoder_path': 'mencoder',
     'animation.writer': 'ffmpeg',
     ...
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看