1、为什么不显示中文:
Matplotlib默认不支持中文字符,因为默认的英文字体无法显示汉字。
图片显示情况:

2、解决方案:
在代码块加上这两行代码:
plt.rcParams[‘font.sans-serif']=[‘SimHei'] #用来正常显示中文标签
plt.rcParams[‘axes.unicode_minus']=False #用来正常显示负号

图片显示情况:

补充jupyter-notebook 中matplotlib不支持中文的问题(Linux)
前言
问题就不截图了, jupyter-notebook中,使用plt画出的图像,中文都是小方格(乱码)
解决方法如下
1. Linux安装字体
把Windows的字体C:/Windows/Fonts复制到Linux系统的存放字体路径/usr/share/fonts下, 如图:

cd到/usr/share/fonts/Fonts,也就是拷贝来的文件夹, 建立字体索引信息,更新字体缓存,让字体生效, 运行命令:
mkfontscale mkfontdir fc-cache -fv source /etc/profile fc-list |wc -l
查看字体fc-list :lang=zh

2. 添加配置
这里有很多版本,比如:
import matplotlib
matplotlib.use('qt4agg')
#指定默认字体
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family']='sans-serif' #解决负号'-'显示为方块的问题
matplotlib.rcParams['axes.unicode_minus'] = False
我的设置是:
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['STSong']
mpl.rcParams['axes.unicode_minus'] = False
如图,可以正常显示中文了

3. 如果还不行,清除缓存
如果添加了第二部的配置,但还是显示中文乱码, 那么有可能是缓存的问题, 执行rm -r ~/.cache/matplotlib

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
您可能感兴趣的文章:- Jupyter Notebook运行JavaScript的方法
- 解决jupyter notebook图片显示模糊和保存清晰图片的操作
- Jupyter notebook 输出部分显示不全的解决方案
- 解决jupyter notebook启动后没有token的坑
- Python中Jupyter notebook快捷键总结
- 解决Jupyter-notebook不弹出默认浏览器的问题
- 终端能到import模块 解决jupyter notebook无法导入的问题
- Jupyter Notebook读入csv文件时出错的解决方案
- Jupyter notebook 更改文件打开的默认路径操作