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

相关推荐
for_ever_love__1 天前
Matplotlib——把训练曲线和分布画出来
matplotlib·数据可视化·大模型开发·训练曲线
1024奇点1 天前
【仓颉语言入门 · 第26课】
开发语言·ide·开源
golang学习记2 天前
VSCode AI新特性:HydraFusion来了:系统会自己组队干活
ide·人工智能·vscode
文祐2 天前
下载Visual Studio 2017 Build Tools version 15.9
ide·visual studio
golang学习记2 天前
Jetbrains 正式官宣:全新AI IDE正式发布!
ide·人工智能·intellij-idea·air
mklpo1472 天前
在win7 sp1 系统上安装最新版 的 Visual Studio Code。2026-10-04
ide·vscode·编辑器
1024奇点3 天前
【仓颉语言入门 · 第24课】
开发语言·ide·开源
程序员清风3 天前
Python 数据分析环境搭建:从 Jupyter 到 pandas
python·jupyter·数据分析
oooost5 天前
Jupyter Notebook 常用快捷键速查
ide·python·jupyter
开开心心_Every5 天前
文件夹批量创建工具支持同级和多层级
运维·服务器·游戏·jupyter·智能手机·pdf·postman