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 小时前
matplotlib多子图与复杂布局实战
python·数据分析·matplotlib·数据可视化
进击的雷神5 小时前
Trae AI IDE 完全指南:从入门到精通
大数据·ide·人工智能·trae
北风toto6 小时前
IDEA模块名字和文件夹名字不一样的解决方式
java·ide·intellij-idea
:1217 小时前
idea17创建tomcat项目(计网底层核心理解!)
java·ide·intellij-idea
猿小猴子7 小时前
主流 AI IDE 之一的 华为云码道「CodeArts」 介绍
ide·人工智能·ai·华为云
非鱼䲆鱻䲜18 小时前
vscode开发stm32添加新的头文件路径和包含源文件
ide·vscode·stm32·cmake·包含头文件·包含源文件
chushiyunen1 天前
pycharm注意力残差示例
ide·python·pycharm
Willliam_william1 天前
QEMU学习之路(11)— 使用VSCode调试qemu-system-riscv64
ide·vscode·学习
LiuYaoheng1 天前
问题记录:Android Studio Low memory
android·ide·android studio
椰猫子1 天前
JDK概述、Maven概述、IDEA概述、常见注解、 Spring Boot + SSM概述、RESTFul编程风格概述
java·ide·intellij-idea