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

相关推荐
猿小猴子6 小时前
主流 Agent 之 「Zed」和 「ZCode」 介绍
ide·ai·agent·zed·zhipu·ade·zcode
浪客川11 小时前
使用 IDEA 生成API文档
java·ide·intellij-idea
SMF191913 小时前
【PyCharm】让 PyCharm 使用 .venv 虚拟环境
ide·python·pycharm
User_芊芊君子14 小时前
STT + LLM + TTS:用一条流水线搓了个中英双向翻译助手
ide·macos·xcode
菜哥万岁万岁万万岁1 天前
Visual Studio 2022 社区版下载
ide·visual studio
iCxhust1 天前
8088单板机VScode集成开发环境使用方法
ide·笔记·vscode·编辑器·微机原理·8088单板机
来日方长。。。。long2 天前
Hermes Agent橙皮书共读|第三篇:保姆级实战部署|本地/VPS从零搭建Hermes
ide·git·hermes
Molesidy2 天前
【VSCode】基于win10系统老版本和VSCode最新版本下出现的VSCode内置的终端窗口不能运行的问题的解决方案
ide·vscode·编辑器
维度攻城狮2 天前
PyCharm 使用 DevContainer 开发:打造一致、隔离、高效的开发环境
ide·python·pycharm·devcontainer
James5062 天前
VSCode+CodeX扩展,无法启动
ide·vscode·codex