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

相关推荐
背心2块钱包邮3 小时前
第5节——定积分与面积
python·深度学习·机器学习·matplotlib
weixin_404679314 小时前
vscode内存过大
ide·vscode·编辑器
BlackPercy5 小时前
[Matplotlib] 动态视频生成
python·matplotlib
sulikey5 小时前
如何使用 Visual Studio 代替 OllyDbg 完成汇编语言实验
汇编·ide·debug·visual studio·ollydbg
七元权5 小时前
VSCode连接远程容器失败(Missing GLIBC >=2.28!)
ide·vscode·编辑器·连接容器
信奥胡老师15 小时前
苹果电脑(mac系统)安装vscode与配置c++环境,并可以使用万能头文件全流程
c++·ide·vscode·macos·编辑器
妖灵翎幺15 小时前
C++ 中的 :: 操作符详解(一切情况)
开发语言·c++·ide
奔跑吧 android15 小时前
【vscode】【远程 ssh 开发】【环境搭建】
ide·vscode·ssh
Maxwell_li115 小时前
Pandas 描述分析和分组分析学习文档
学习·数据分析·numpy·pandas·matplotlib
森明帮大于黑虎帮20 小时前
Visual Studio 2008 自述文件
ide·visual studio