jupyter matplotlib 中文报错/乱码

在 jupyter 中使用 matplotlib 进行画图的时候, 如果包含中文, 会有一堆报错, 并且输出的图片中的中文也无法正常显示.

解决方案:

  1. 查找支持的中文字体
py 复制代码
import matplotlib.font_manager as fm

# matplotlib only know these fonts
font_list = [f for f in fm.fontManager.ttflist]

# check font names what you want
# 'CJK', 'Han', , 'TW'
cjk_list = ['CN']

for f in font_list:
    if any(s.lower() in f.name.lower() for s in cjk_list):
        print(f'name={f.name}, path={f.fname}')

输出结果:

复制代码
name=AR PL UMing CN, path=/usr/share/fonts/cjkuni-uming/uming.ttc

如果没有结果, 则需要去下载安装字体

  1. 设置字体

在输出结果中, 找一个 name 的值进行设置:

py 复制代码
%matplotlib inline

plt.rc('font', family='AR PL UMing CN')

matplotlib 的图中, 中文就可以正常显示了

如果依然不行, 尝试删除 rm -rf ~/.matplotlib/font* or rm -rf ~/.cache/.matplotlib/fontl*. 重启 jupyter

相关推荐
萌萌站起3 小时前
Vscode 中 python模块的导入问题
ide·vscode·python
王夏奇4 小时前
pycharm中3种不同类型的python文件
ide·python·pycharm
ShawnLiaoking5 小时前
pycharm 上传更新代码
ide·elasticsearch·pycharm
代码小书生6 小时前
Matplotlib,Python 数据可视化核心库!
python·信息可视化·matplotlib
Freak嵌入式8 小时前
MicroPython LVGL基础知识和概念:GUI 的扩展接口
ide·驱动开发·嵌入式·gui·lvgl·micropython·upypi
绛橘色的日落(。・∀・)ノ12 小时前
Matplotlib第六章场景案例显神通
matplotlib
蜡笔小新..12 小时前
Linux下Matplotlib使用Times New Roman字体的解决方案
linux·运维·matplotlib
无限进步_12 小时前
【C++】重载、重写和重定义的区别详解
c语言·开发语言·c++·ide·windows·git·github
ZXF_H13 小时前
VSCode C/C++函数Ctrl+鼠标点击无法跳转的解决方法
c++·ide·vscode
小鱼~~13 小时前
Jupyter Notebook 最常用快捷键
python·jupyter