最近將電腦的操作系統(tǒng)由WIN7更改為了UOS操作系統(tǒng),在設(shè)置好python運(yùn)行環(huán)境后,原來用matplotlib繪圖時中文一直顯示不出來,參考網(wǎng)上N多方案后,經(jīng)實(shí)踐,以下解決方案可行:
1、首先查看matplotlib字體的位置:
在python環(huán)境下輸入
import matplotlib
print(matplotlib.matplotlib_fname())
我的字體配置文件位置為:/data/home/bbr/tools/anaconda3/envs/uhi/lib/python3.8/site-packages/matplotlib/mpl-data/matplotlibrc
2、將下載好的simhei.ttf存入文件夾
文件夾地址 /data/home/bbr/tools/anaconda3/envs/uhi/lib/python3.8/site-packages/matplotlib/mpl-data/fonts/ttf
3、刪除字體緩存,執(zhí)行如下操作
cd ~/.cache/matplotlib
rm -rf *
4、修改配置文件
vi matplotlibrc
font.family : sans-serif #去掉該行前面的#
font.serif : simhei,... #添加simhei
最后,重新啟動下python,繪圖時就能正常顯示中文了。