美文网首页
matplotlib显示中文问题

matplotlib显示中文问题

作者: 代码没写完还想上厕所 | 来源:发表于2021-01-26 14:46 被阅读0次

    由于使用的是mac,这几天使用 matplptlib遇到显示中文问题

    原设置字体如下:

    # 指定默认字体
    plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    # 解决保存图像是负号'-'显示为方块的问题
    plt.rcParams['axes.unicode_minus'] = False
    
    显示不了中文.png

    原因:

    • matplotlib第三方库中没有中文字体包
    • 查看matplotlib所有字体:
    from matplotlib import font_manager
    a = sorted([f.name for f in font_manager.fontManager.ttflist])
    for i in a:
        print(i)
    

    解决办法:

    • 网络下载中文字体包 ttf 格式
    • 我的路径/usr/local/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/tff
    路径.png
    • 将下载的对应的ttf文件放入ttf文件夹中
    • 重新加载matplotlib的字体库
    from matplotlib.font_manager import _rebuild
    _rebuild()
    
    重新加载.png

    搞定收工

      > 可以通过上面查看matplotlib字体的步骤  查看有没有加载成功和对应的名字 
    

    相关文章

      网友评论

          本文标题:matplotlib显示中文问题

          本文链接:https://www.haomeiwen.com/subject/qspgzktx.html