環境
pyenv python =3.7.0
matplotlib = 3.4.3
OS = Ubuntu 16.04
報錯
文字會顯示為一些小方框

由於已解決問題,無此顯示效果,圖片引用自
matplotlib輕松解決中文亂碼問題
解決
查看matplotlib配置文件位置,方便后續配置
In [1]: import matplotlib
In [2]: matplotlib.matplotlib_fname()
Out[2]: '/root/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc'
首先查看系統內是否有中文字體
fc-list :lang=zh
# 如果沒有fc-list命令,則通過以下命令行進行安裝
apt install fontconfig

如果沒有回顯,可以在本地找一個,或者網上下載一個
SimHei

上傳到/usr/share/fonts路徑下創建自己的字體文件夾,並將文件傳入此文件夾中,如果沒有此文件夾創建。
cd /usr/share/fonts/yourfontdir
mv simhei.ttf /usr/share/fonts/yourfontdir
上傳完畢,在服務器中安裝存放的字體,mkfontscale 和 mkfontdir
apt install fontconfig mkfontscale
#生成字體索引信息. 會顯示字體的font-family
sudo mkfontscale
sudo mkfontdir
fc-cache #更新字體緩存
然后修改matplotlibrc文件配置
cp /usr/share/fonts/yourfontdir/simhei.ttf /root/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/mpl-data/matplotlibrc
#刪除緩沖目錄
rm -rf ~/.cache/matplotlib
#如果有字體,繪圖還是顯示小方塊,則需要強力刪除
rm -rf ~/.matplotlib/*.cache
此時字體已經存在於服務器,配置改了之后不會生效,需要在python內重新加載,即:
python console:
from matplotlib.font_manager import _rebuild
_rebuild() #reload一下
我在此matplotlib版本沒有找到_rebuild,重啟jupyter即可生效,中文效果圖如下