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

相关推荐
文 丰7 小时前
【Android Studio】gradle下载慢解决方案(替换配置-非手工下载安装包)
android·ide·android studio
Jackson@ML10 小时前
2026最新版Visual Studio Code安装使用指南
ide·vscode·编辑器
New_Horizons66611 小时前
VScode 无法使用shift + F12查看引用
ide·vscode·编辑器
tc&11 小时前
VSCode远程连接AlmaLinux虚拟机问题总结
ide·vscode·编辑器
web守墓人12 小时前
【编辑器】简单了解下vscode的go语言插件原理
ide·vscode·编辑器
颖风船1 天前
vscode连接vmware中的deepin虚拟机,deepin端配置
linux·ide·vscode
小胖红2 天前
Xcode 打包失败 处理
ide·macos·xcode
1024小神2 天前
Vscode/Cursor中的Prettier插件格式化降级操作
ide·vscode·编辑器
102400242 天前
vscode无法选择conda虚拟环境下的解释器
ide·vscode·conda
凯子坚持 c2 天前
在 Visual Studio 2019 中配置 Qt 插件开发环境:编译原理 + 底层逻辑解析
ide·qt·visual studio