python matplotlib linux中文亂碼問題

在window下 matplotlib 圖表顯示的是沒有問題的。

畫圖時加上下面一句就能防止亂碼。

# 設置中文顯示,否則出現亂碼!

plt.rc('font', family='SimHei', size=12)

但上傳到linux上就中文全顯示成口字了。

解決方案;

1.下載simhei.ttf放入你的python環境下的matplotlib的ttf路徑下,我這邊路徑如下:

/usr/local/lib/python3.6/dist-packages/matplotlib/mpl-data/fonts/ttf

2.打開matplotlib/mpl-data/matplotlibrc這個文件

找到font.sans-serif,打開他,然後字前面加上SimHei,加在最前面。

font.sans-serif     : SimHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif, Microsoft YaHei

其他的都不需要修改了。

SimHei下載地址:https://www.fontpalace.com/font-download/SimHei/

也可以自己百度搜索下載。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章