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

相关推荐
AI小云33 分钟前
【数据操作与可视化】Matplotlib绘图-生成其他图表类型
开发语言·python·matplotlib
baidu_172012531 小时前
在Visual Studio中安装通义灵码
ide·visual studio
黎相思5 小时前
环境搭建
ide
晨同学03275 小时前
【亲测可行】windows安装visual studio & opencv4.10.0
ide·windows·visual studio
PWRJOY5 小时前
Android Studio中安卓模拟器打不开,报错The emulator process for AVD has terminated
android·ide·android studio
冷雨夜中漫步10 小时前
AI入坑之路——(1)搭建本地的Python与Jupyter开发环境
人工智能·python·jupyter
BD_Marathon10 小时前
【IDEA】IDEA的详细设置
java·ide·intellij-idea
帮帮志11 小时前
Jupyter使用的快捷键大全
ide·python·jupyter
qq_4639448611 小时前
Jupyter中输入标题的方法
ide·python·jupyter
Xiaok101812 小时前
VSCode 报错 “No module named ‘torch‘“
ide·vscode·编辑器