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

相关推荐
小小荧1 天前
VSCode 推出绿色版!更强!更智能!
ide·vscode·编辑器
GawynKing1 天前
SqlRest数据服务项目基于Idea开发环境搭建
java·ide·intellij-idea
酷飞飞1 天前
使用 WSL + VSCode 搭建 ESP32/ESP32-S2 开发环境(2026 最新版)
ide·vscode·编辑器
亚里随笔1 天前
vscode mac常用技巧—快捷键
ide·vscode·macos
时光Autistic2 天前
【环境配置】安装LaTeX并配置到PyCharm使用
ide·python·pycharm·latex
夏幻灵2 天前
写给初次用IDEA的新人
java·ide·intellij-idea
环黄金线HHJX.2 天前
【拼音字母量子编程语言AiPQL】
开发语言·ide·人工智能·算法·编辑器·量子计算
努力的小雨2 天前
从“Agent 元年”到 AI IDE 元年——我的2025
ide·人工智能
liu****3 天前
04_Pandas数据分析入门
python·jupyter·数据挖掘·数据分析·numpy·pandas·python常用工具
怪力左手4 天前
vscode扩展离线下载
ide·vscode·编辑器