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

相关推荐
Tian_Hang1 天前
Eclipse Ditto 的权限策略
java·服务器·前端·网络·ide·ubuntu·eclipse
yang_coder1 天前
juypter notebook启动ssl报错的处理记录
python·jupyter
Tian_Hang1 天前
Eclipse Mosquitto 安装及介绍
java·运维·服务器·ide·sql·mysql·eclipse
星云_byto1 天前
五分钟从零配置好VSCode + Claude Code + DeepSeek V4 Pro
ide·vscode·编辑器·cursor·codex·claude code·deepseekv4
诚信定制8392 天前
Typora插件开发指南:打造专属IDE式写作环境
ide
Tian_Hang2 天前
Eclipse Ditto 物模型相关代码
java·运维·服务器·ide·eureka·eclipse
Tian_Hang2 天前
Eclipse Ditto 节流机制
java·ide·eclipse
DogDaoDao2 天前
AI 编程 IDE 全景解析 2026:Agent 全面接管开发链路
ide·人工智能·程序员·ai编程·claude·cursor·ai agent
林熙蕾LXL2 天前
VSCode调试
ide·vscode·编辑器
测试工程师成长之路2 天前
Typora插件开发指南:打造专属IDE式写作环境
ide