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 小时前
利用vscode时进行调试,即使设置justMyCode为False仍然失败,如何解决?
ide·vscode·编辑器·justmycode
Zeluar11 小时前
关闭VSCode Markdown插件在Jupyter Notebook中的自动预览
ide·vscode·jupyter
海绵不是宝宝81719 小时前
连接远程服务器上的 jupyter notebook,解放本地电脑
服务器·jupyter·github
雷工笔记1 天前
【软件安装】VScode介绍安装步骤及中文界面设置方法
ide·vscode·编辑器
△曉風殘月〆1 天前
Visual Studio中的常用调试功能(下)
c++·ide·visual studio·调试
△曉風殘月〆1 天前
Visual Studio中的常用调试功能(上)
ide·visual studio·调试
WSSWWWSSW1 天前
Matplotlib数据可视化实战:Matplotlib子图布局与管理入门
python·信息可视化·matplotlib
WSSWWWSSW1 天前
Matplotlib数据可视化实战:Matplotlib图表美化与进阶教程
python·信息可视化·matplotlib
翁正存1 天前
IDEA测试代码报java file outset source root异常
java·ide·intellij-idea
一枚小小程序员哈2 天前
基于Android的随身小管家APP的设计与实现/基于SSM框架的财务管理系统/android Studio/java/原生开发
android·ide·android studio