1. 程式人生 > 實用技巧 >Mac如何解決matplotlib發生圖例中文亂碼問題

Mac如何解決matplotlib發生圖例中文亂碼問題

操作環境

anaconda

python3.7

1.下載字型SimHei並完成安裝

下載連結: https://www.fontpalace.com/font-download/SimHei/

2. 複製字型到matplotlib字型目錄下的ttf檔案裡

anaconda3⁩/pkgs⁩/⁨matplotlib-base-3.1.3-py37h9aa3819_0⁩/lib⁩/python3.7⁩/⁨site-packages⁩/matplotlib⁩/⁨mpl-data⁩/fonts⁩/ttf⁩

3. 修改matplotlibrc檔案內容

anaconda3⁩/pkgs⁩/⁨matplotlib-base-3.1.3-py37h9aa3819_0⁩/lib⁩/python3.7⁩/⁨site-packages⁩/matplotlib⁩/⁨mpl-data⁩

開啟檔案:

(1) 將以下font.familyfont.sans-serif前面的#去掉

(2) 在font.sans-serif:後面新增SimHei

(3) 同時將找到axes.unicode_minus,將True改為False,作用就是解決負號'-'顯示為方塊的問題。

操作完成如下

font.family : sans-serif
font.sans-serif: SimHei(新安裝的字型), DejaVu Sans, Bitstream Vera Sans, Lucida Grande,
Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
#axes.unicode_minus : False

4. 刪除tex.cache和fontList.json

5. 重啟執行JupyterNotebook,即可看到中文

from: https://zhuanlan.zhihu.com/p/37259909