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

相关推荐
wjhx7 小时前
关于xcode中使用图标
ide·macos·xcode
漫随流水10 小时前
创建一个IDEA的Java项目
java·ide·intellij-idea
且去填词12 小时前
VSCode 中使用 Codex:命令、Agent 与 Skills 完整指南
ide·人工智能·vscode·编辑器·codex
PersistJiao13 小时前
开发环境对比:VS Code、Cursor、IntelliJ IDEA
java·ide·intellij-idea
渣渣灰958713 小时前
VSCode开发环境开发Qt程序
ide·vscode·qt
winfred_hua14 小时前
PyCharm中使用jupyter
ide·jupyter·pycharm
仅此,14 小时前
vscode 启动项目时,设置 PYTHONPATH 导包路径
ide·vscode·python·编辑器
svdo1250p14 小时前
“Fatal error: require(): Failed opening required...” 以及如何彻底避免它再次出现
android·ide·android studio
lulu12165440781 天前
JetBrains IDE 终极AI编程方案:CC GUI插件让Claude Code和Codex丝滑运行
java·ide·人工智能·python·ai编程
平凡码工人1 天前
vscode 安装教程 2026
ide·vscode·编辑器