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

相关推荐
yz_aiks17 小时前
IDEA终端配置oh-my-zsh实战:安装、插件与日常使用技巧
java·ide·intellij-idea
小yu学编程18 小时前
IDEA 2025版本中如何设置包层级结构
java·ide·intellij-idea·层级结构
YXWik618 小时前
CodeGraph安装及在idea的claude code插件中使用
java·ide·intellij-idea
wbc1031555818 小时前
基于 VSCode + Icarus 的 Verilog 编译和仿真
ide·vscode·编辑器
谷哥的小弟21 小时前
(最新版)VSCode安装图文详解教程
ide·vscode·编辑器·教程·前端开发·图文
DogDaoDao21 小时前
OpenCV 踩坑全指南
图像处理·人工智能·python·opencv·计算机视觉·matplotlib·rgb
IOT.FIVE.NO.11 天前
Codex+Vscode+Remote ssh+ 服务器自定义第三方API配置保姆级教程
ide·vscode·编辑器
ai_coder_ai1 天前
使用web ide开发和调试自动化脚本
前端·ide·自动化
香菇滑稽之谈1 天前
VSCode配置QT环境
ide·vscode·qt
电魂泡哥2 天前
Matplotlib.pyplot 完全入门指南
信息可视化·matplotlib