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

相关推荐
啊湘2 小时前
vscode 使用 github (适用CURSOR等使用)
ide·vscode·github·cursor·mcp
从此不归路3 小时前
FPGA 结构与 CAD 设计(第3章)下
ide·fpga开发
云堆客栈4 小时前
基于vscode的markdown编辑器部署方案
ide·vscode·编辑器
smileNicky4 小时前
Lombok @Data 在 IDEA 中运行报错解决方案
java·ide·intellij-idea
好大哥呀4 小时前
C++ IDE
开发语言·c++·ide
dust_and_stars5 小时前
ubuntu24 安装vscode
ide·vscode·编辑器
向上的车轮5 小时前
如何选择Python IDE?
开发语言·ide·python
Destiny_where5 小时前
Claude VSCode插件版接入强大的GLM(无需登录注册claude code)
ide·人工智能·vscode·编辑器·claude code
猫头虎6 小时前
Claude Code 2026 年1月9日迎来大更新:Agent 能力增强(2.1.0 详解 + 升级指南)
ide·人工智能·macos·langchain·编辑器·aigc·ai编程
__雨夜星辰__6 小时前
VScode免密远程连接到Ubuntu(VMware17环境)虚拟机
ide·vscode·ubuntu