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

相关推荐
Albert Edison8 小时前
PyCharm 字体大小设置
ide·python·pycharm
Super 含1 天前
Android 包体积优化(一):APK 到底大在哪里?从 APK 结构到体积分析
android·ide·vscode
冷雨夜中漫步1 天前
IDEA CodeStyle代码风格格式化模板
java·ide·intellij-idea
Promising_GEO2 天前
新电脑科研环境配置指南:Miniforge + PyCharm + GitHub 从安装到可用
ide·python·pycharm·github·地理
舞动青春882 天前
vscode copilot配置kimi k3
ide·vscode·copilot
爱上纯净的蓝天2 天前
AtomCode 多语言开发支持:一个 AI IDE 搞定全栈开发
ide·多语言·全栈开发·atomcode
小小啊python3 天前
IDEA中SpringBoot项目配置热部署
java·ide·intellij-idea·springboot·热部署
树码小子3 天前
Pycharm解释器配置问题解决
ide·python·pycharm
FlyWIHTSKY4 天前
idea中集成claude功能
java·ide·人工智能·intellij-idea·cloudera
weixin_404679314 天前
godot + vscode ai开发
ide·vscode·编辑器·游戏引擎·godot