方法一、修改matplotlibrc配置字体
1. 定位matplotlib的字体库路径
locate -b '\mpl-data'
或者在 python环境下
import matplotlib
matplotlib.matplotlib_fname()
/cnn/.pyenv/versions/3.6.5/envs/env3web/lib/python3.6/site-packages/matplotlib/mpl-data
2. 将微软雅黑字体文件msyh.ttf,拷贝到该目录的fonts/ttf下
cp /mnt/share/msyh.ttf /cnn/.pyenv/versions/3.6.5/envs/env3web/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf
3. 删除 matplotlib 缓存
cd ~/.cache/matplotlib
rm * -r
4. 修改matplotlibrc文件:修改font.sans-serif 加微软雅黑字体
font.sans-serif : Microsoft YaHei, DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif
5. 重启程序或在 jupyter中restart the kernel
方法二、用plt.rcParams设置
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Microsoft YaHei']
plt.xlabel('测试')
plt.show()
方法三、用FontProperties,并指定字体文件
from matplotlib.font_manager import FontProperties
import matplotlib.pyplot as plt
font = FontProperties(fname='/mnt/share/code/font/MSYH.TTC', size=20)
plt.title('测试', fontproperties=font)
字体安装
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv
会将 /usr/share/fonts 下的字体安装一遍
查看系统中文字体
fc-list :lang=zh
pandas_profiling 中文字体设置
# 没起作用
pip show --verbos pandas_profiling # 找到 pandas_profiling 所在位置
vi visualisation/pandas_profiling.mplstyle
# 也加这段话起作用
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Microsoft YaHei']