matplotlib库

乱码问题

matplotlib本身只支持ASCII,所以需要更改字体库
有几种方法可以解决中文乱码,这里采用更改配置一次到位
以下是mac环境,其他环境类似

  1. 查看字体文件注册名字

    1
    2
    3
    import matplotlib
    from matplotlib import font_manager
    sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])

    根据list找到对应字体库的注册名字…这里建议找能对应的上的

  2. 把对应字体tff文件拷贝到,matplotlib安装目录下的mpl-data/fonts/tff中
  3. 更改mpl-data/matplotlibrc

    1
    2
    3
    4
    5
    #font.family         : sans-serif
    #font.style : normal
    #font.variant : normal
    #font.weight : medium
    #font.stretch : normal

    改成

    1
    font.family         : 对应的注册名

    即可