zoukankan      html  css  js  c++  java
  • 解决mac上matplotlib中文无法显示问题

    系统:mac os, high sierra; python3.7(by brew installed)
     
    在网上找了很多基本上都是让下载SimHei字体,然后放到mac的matplotlib的字体库,然后改配置,然而。。。亲测无效。。。
     
     
    于是。。。为什么要费这些功夫呢,直接找找mac底下有哪些支持中文的字体库不就好了嘛。。。然后发现Arial Unicode MS,亲测可用,代码如下:
    plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
     
     

    永久更改方法:

    找到字体路径(跟其他教程一样)
    进入python
    import matplotlib
    print(matplotlib.matplotlib_fname())
    进入该路径,我的是在:/usr/local/lib/python3.7/site-packages/matplotlib/mpl-data
     
    然后编辑文件matplotlibrc
    可以直接在最下面加上三行:
    font.family: sans-serif
    font.sans-serif: Arial Unicode MS, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
    axes.unicode_minus: False
    然后把~/.matplotlib目录下的两个文件删除掉(缓存文件可以放心删): rm -rf ~/.matplotlib/* 
     
    重启python
    即可解决中文及负号无法正常解决的问题。
     
     
    可测试:
    x = ['张三', '李四', '-1']
    y = [1, 2, 3]
    plt.plot(x, y)
    plt.show()
     
     

  • 相关阅读:
    blktrace分析IO
    Mac-配置SecureCRT
    Mac-安装itellij idea
    Mac-sublime text 3破解版
    Mac-item+zsh
    Mac-安装homebrew
    Mac-装机
    Mac-WIFI总是断网
    Git-ssh登录github
    Git-回滚操作
  • 原文地址:https://www.cnblogs.com/cymwill/p/10554916.html
Copyright © 2011-2022 走看看