解决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()
 
 

原文地址:https://www.cnblogs.com/cymwill/p/10554916.html