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

相关推荐
番茄去哪了12 小时前
苍穹外卖day07---Redis缓存优化与购物车功能实现
java·数据库·ide·spring boot·spring·maven·mybatis
人生导师yxc16 小时前
IDE缓存配置等位置更改(自存)
java·ide·intellij-idea
KvPiter2 天前
一人软件公司 《solopreneur》从0到1
ide·人工智能
※DX3906※2 天前
Java多线程3--设计模式,线程池,定时器
java·开发语言·ide·设计模式·intellij idea
Tisfy2 天前
Windows - VsCode导致Windows凭据过多之一键删除
ide·windows·vscode
学编程的闹钟2 天前
安装GmSSL3库后用VS编译CMake源码
c语言·c++·ide·开发工具·cmake·visual studio
BD_Marathon2 天前
IDEA创建多级包时显示在同一行怎么办
java·ide·intellij-idea
姜源Jerry2 天前
【Trae】Trae IDE&SOLO浅尝
java·ide·ai
xzjiang_3653 天前
Jupyter 运行经验3:读入和显示一张图片
ide·python·jupyter
我命由我123453 天前
Visual Studio 文件的编码格式不一致问题:错误 C2001 常量中有换行符
c语言·开发语言·c++·ide·学习·学习方法·visual studio