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+Cline部署本地爬虫fetch-mcp实战
ide·vscode·爬虫·ai·mcp
名剑走天下4 小时前
在VSCode中:解决终端输出中文乱码问题
ide·vscode·编辑器
兰文彬4 小时前
vscode 安装Jupiter以后,终端无法切换conda环境了
ide·vscode·编辑器
CV小白进阶路4 小时前
【无标题】Vscode 报错 got bad result from install script无法远程链接服务器
服务器·ide·vscode
名剑走天下4 小时前
在 VSCode 中:引入开源cJSon解析库+示例demo
ide·vscode·编辑器
我命由我123456 小时前
IDEA - IDEA 快速回到页面首尾、页面快速滑动、快速定位到指定行
java·运维·ide·后端·java-ee·intellij-idea·intellij idea
Mickyจุ๊บ6 小时前
IDEA 插件推荐
java·ide·intellij-idea
命运之光6 小时前
【快速解决】idea运行javafx错误: 缺少 JavaFX 运行时组件, 需要使用该组件来运行此应用程序
java·ide·intellij-idea
mrsyf19 小时前
VSCode中Copilot的询问、编辑、代理有啥区别?
ide·vscode·copilot