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

相关推荐
小魏小魏我们去那里呀4 分钟前
Java2Flowchart:一款把 Java 方法一键转换成 Mermaid 流程图的 IntelliJ 插件
java·ide·intellij-idea
WHS-_-202221 分钟前
Pycharm 使用经验
ide·python·pycharm
AIBox3651 小时前
vscode api 配置怎么做:第三方大模型接入 VS Code 的完整方法
ide·人工智能·vscode·gpt·语言模型·编辑器
青苔猿猿2 小时前
【3】jupyter单元格Cell操作
python·jupyter·单元格
sz4972385992 小时前
WSL2+VSCode搭建ESP-IDF 开发环境
ide·vscode·编辑器·esp32·wsl
星马梦缘2 小时前
强化学习实战8.1——用PPO打赢星际争霸【环境配置与下位机代码】
人工智能·python·jupyter·强化学习·星际争霸·stablebaseline3·starcraft2
Stone_OverLooking4 小时前
vscode 配置Qt qmake项目
ide·vscode·qt
ฅ ฅBonnie4 小时前
使用 VSCODE 连接远程服务器上的容器
服务器·ide·vscode
l1t4 小时前
uv提供的cpython高版本已经解决了matplotlib无法显示图形问题
matplotlib·uv
Predestination王瀞潞5 小时前
彻底解决IDEA Console控制台乱码(Python可供参考第一部分)
java·ide·intellij-idea