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

相关推荐
独隅5 小时前
Visual Studio 2026 详细安装教程和配置指南
ide·visual studio
lifewange10 小时前
Pycharm和IDEA中安装Cursor的方法
ide·pycharm·intellij-idea
读书札记20221 天前
visual studio 调试技巧总结
ide·visual studio
hacker7071 天前
Visual Studio安装教程(C#开发版)
ide·c#·visual studio
XD7429716361 天前
科技早报晚报|2026年5月1日:本地优先文档、安卓离线 IDE 与双击即用密码库,今天最值得跟进的 3 个机会
android·ide·科技·科技新闻·开发者工具·本地优先
謓泽1 天前
【Trae IDE】核心功能详解与使用教程
ide·ai·trea
袋子(PJ)1 天前
2026年pytorch基础学习(基于jupyter notebook开发)——从原理到落地:PyTorch神经网络架构与工程优化解析
人工智能·pytorch·深度学习·学习·jupyter
lzl20401 天前
VSCode中Codex CLI登录卡在‘Sign in with ChatGPT‘屏幕
ide·vscode·chatgpt·codex
byzh_rc2 天前
[AI工具从入门到入土] 命令行
网络·人工智能·python·深度学习·matplotlib
AI进化营-智能译站2 天前
Jazzy ROS2入门指南系列05-配置VsCode实现ROS2项目开发
ide·vscode·ai·编辑器