解決linux服務器上matplotlib中文顯示亂碼問題


報錯信息: UserWarning: findfont: Font family [u'sans-serif'] not found. Falling back to DejaVu Sans

找不到字體,在繪制的圖片中中文顯示亂碼

解決方法:

1. 查看配置路徑:

import matplotlib 
print (matplotlib.matplotlib_fname())

2. 下載字體:simhei.tff : http://www.font5.com.cn/font_download.php?id=151&part=1237887120

3. 將字體拷貝到 tff 目錄下:

/home/hwy/anaconda3/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/tff/

5. 刪除字體緩存文件:

首先找到字體緩存文件的位置:

import matplotlib as mpl
fm = mpl.font_manager
fm.get_cachedir()

這是我緩存文件的位置:

/home/hwy/.cache/matplotlib/

刪掉就好了:

rm -rf /home/hwy/.cache/matplotlib/

6. 字體參數設置:

修改配置文件 matplotlibrc:

將一下三句修改,並將 # 去掉:

font.family         : sans-serif 
...
font.sans-serif     : SimHei
...
axes.unicode_minus  : False # use unicode for the minus symbol

意思就是知道字庫族為 sans-serif,同時添加“SimHei”即宋體到字庫族列表中,同時將找到axes.unicode_minus,將True改為False,作用就是解決負號’-‘顯示為方塊的問題。

成功顯示中文了!!

下面的博客配置的蠻好的。

http://xiaqunfeng.cc/2018/03/13/mac%E4%B8%8AMatplotlib%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM