解決matplotlib在MAC系統下中文字型顯示問題報錯:Font family [‘sans-serif‘] not found.Falling back to DejaVu Sans.
阿新 • • 發佈:2021-12-09
報錯
findfont: Font family [u'sans-serif']notfound. Falling back to Bitstream Vera Sans
這是因為matplotlib 是沒有中文字型的,需要我們手動安裝
在網上搜索瞭解到基本上大家都是在安裝matplotlib後才會遇到報錯,若我們mac上沒有安裝matplotlib 可以在python終端下輸入 pip install matplotlib 進行安裝
- 執行命令列終端,輸入’python‘進入python編輯 - 輸入 import matplotlib - 輸入 matplotlib.matplotlib_fname() 目錄在 .../matplotlib/mpl-data/fonts/ttf中,前面的路徑會因為每個人的python安裝路徑不同有所區別,我本人的字型存放目錄的完整路徑如下 /usr/local/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf
1.可以在這個地址下載
https://www.fontpalace.com/font-download/SimHei/
2.下載好之後把下載個好的字型放到ttf資料夾下
command + shit+g
把上面那個檔案路徑輸入
3.將matplotlib的字型快取檔案刪除
cd ~/.matplotlib/
rm -rf fontList.py3k.cache
4.有的人在刪除快取重啟一下Python之後就可以了,但是我的不行
在這裡看到大佬的文章修改/site-packages/matplotlib/mpl-data/matplotlibrc
但是我開啟這個檔案是亂碼,所以用不了這個方法,
附上鍊接:
https://zhuanlan.zhihu.com/p/40931776
5.最後找到了一個臨時的解決方法
在Python程式碼中新增以下程式碼
# fname 為字型檔案路徑 import matplotlib myfont = matplotlib.font_manager.FontProperties( fname=r'/usr/local/lib/python3.9/site-packages/matplotlib/mpl-data/fonts/ttf/SimHei.ttf')
連結:https://www.cnblogs.com/bhlsheji/p/5349786.html
重新執行一下專案