看了很多大佬的作法, 但是運氣不佳, 沒成功於事我再繼續爬文, 試了幾遍, 終於成功了, 我整理一下, 貼上來與大家分享
1. 在 jupyter 輸入如下代碼,
import matplotlib
print(matplotlib.matplotlib_fname())
/Users/Adam/opt/anaconda3/lib/python3.9/site-packages/matplotlib/mpl-data/matplotlibrc
2. 使用文本編輯軟件編輯 matplotlibrc, 尋找三組關鍵字
1. font.family: 去除這組字前面的 #
2. font.sans-serif: 去除這組字前面的 #, : 後加一組 SimHei,
3. axes.unicode_minus: 去除這組字前面的 #, : 後的 True 改為 False
4. 保存, 退出.
3. 下載 SimHei.ttf 字體
4. 放到與 matplotlibrc 同目錄內的 fonts 目錄下得 ttf 目錄內.
5. 退出重啟 jupyter
6. 網上常說的 Simhei 我是的結果還是框框, 改用 Arial Unicode MS 就可以了.
import matplotlib.pyplot as plt
# plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["font.sans-serif"] = ["Arial Unicode MS"]
import random
x=[0,1,2,3,4]
y=[random.randint(1,100) for i in range(5)]
plt.plot(x,y)
plt.xlabel("x軸數據")
plt.ylabel("y軸數據")
plt.show()