当前位置 博文首页 > matplotlib相关系统目录获取方式小结

    matplotlib相关系统目录获取方式小结

    作者:mighty13 时间:2021-07-29 18:43

    一、获取matplotlib的安装位置

    导入matplotlib,打印__file__属性,即可显示matplotlib包的安装位置。

    In [1]: import matplotlib
    In [2]: matplotlib.__version__
    Out[2]: '3.3.2'
    In [3]: matplotlib.__file__
    Out[3]: 'd:\\ProgramData\\Anaconda3\\lib\\site-packages\\matplotlib\\__init__.py
    
    

    二、获取matplotlib的配置目录

    配置目录获取比较复杂,遵循以下规律:
    如果设置了MPLCONFIGDIR 环境变量,那么配置目录就是该变量对应目录。如果没有选择,那么配置目录为$HOME/.matplotlib。

    In [4]: matplotlib.get_configdir()
    Out[4]: 'C:\\Users\\adminstrator\\.matplotlib'
    
    

    三、获取matplotlib的缓存目录

    一般情况下,get_cachedir()和get_configdir()返回同一个目录,特例是在linux中,如果设置环境变量$XDG_CACHE_HOME/$HOME/.cache,则使用环境变量设置的目录。
    matplotlib的字体缓存存放在该目录。

    In [5]: matplotlib.get_cachedir()
    Out[5]: 'C:\\Users\\adminstrator\\.matplotlib'

    四、获取matplotlib的配置文件路径

    matplotlib_fname()获取的即配置文件matplotlibrc文件所在位置。

    In [6]: matplotlib.matplotlib_fname()
    Out[6]: 'd:\\ProgramData\\Anaconda3\\lib\\site-packages\\matplotlib\\mpl-data\\m
    atplotlibrc'

    五、获取matplotlib的数据文件目录

    matplotlib安装时附带了一些数据,比如字体、导航界面图片、样本数据、绘图风格等,在Windows平台中,rc文件默认也保存在该目录。该目录结构如下:

    ├─mpl-data
    │ ├─fonts
    │ │ ├─afm
    │ │ ├─pdfcorefonts
    │ │ └─ttf
    │ ├─images
    │ ├─sample_data
    │ │ └─axes_grid
    │ └─stylelib
    
    
    In [7]: matplotlib.get_data_path()
    Out[7]: 'd:\\ProgramData\\Anaconda3\\lib\\site-packages\\matplotlib\\mpl-data'
    
    
    jsjbwy