jupyter notebook 安装字体解决可视化时字体不显示

jupyter notebook 安装中文字体解决可视化时部分字体不显示或者错位

1
2
3
4
5
6
7
%matplotlib inline
plt.rcParams['figure.figsize'] = (12.0, 8.0) # 调整大小,可根据自实际情况进行设置
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
plt.rcParams['font.sans-serif']=['SimHei'] # 下面这两个是设置乱码的
plt.rcParams['axes.unicode_minus']=False
plt.style.use('ggplot') # 使用'ggplot'风格美化显示的图表

jupyter中的中文字体显示框框,解决方案:

1、下载simhei.ttf字体:

1
wget http://d.xiazaiziti.com/en_fonts/fonts/s/SimHei.ttf

2、将字体放到合适的地方

1
sudo cp SimHei.ttf /usr/share/fonts/

3、删除matplotlib缓存 – 在jupyter中编辑
查看缓存文件在哪里

image-20240602134946681

1
2
cd /home/louis/.cache/matplotlib
rm *

4.修改matplotlib的配置– 在jupyter中编辑

image-20240602135342971

到终端,编辑该文件matplotlibrc,添加:

1
2
3
font.family:sans-serif
font.sans-serif:SimHei
axes.unicode_minus:False

5.jupyter重启服务