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

相关推荐
向上的车轮16 分钟前
Typora插件开发指南:打造专属IDE式写作环境
ide·typora插件
唯刻V1 小时前
你的IDE已经不认识你了
android·ide·android-studio·cli
彷徨而立1 小时前
【Visual Studio】msbuild 使用举例
ide·visual studio
lazy H1 小时前
IDEA 如何配置 JDK?项目 SDK 报错解决方法
java·ide·后端·学习·intellij-idea
OsDepK12 小时前
移动编程OSMDE随时命令行进行Git
ide·git
色空大师16 小时前
【debug调试详解-idea】
java·ide·intellij-idea·调试·远程调试
Tiger Z19 小时前
Positron 教程7 --- 工作区
ide·编程·positron
木囧20 小时前
PyCharm手动创建虚拟环境
ide·python·pycharm
爱就是恒久忍耐1 天前
VSCode里clangd如何配置跳转
ide·vscode·编辑器
SmartManWind1 天前
vscode中claude-code插件wsl中开启自动模式
ide·vscode·编辑器·claude code