系統: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()
