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

相关推荐
TonyLee0177 小时前
VSCode使用SSH FS插件进行远程连接(适配远程低版本系统)
ide·vscode·ssh
爱喝可乐的老王8 小时前
数据分析实践--数据解析购房关键
信息可视化·数据分析·pandas·matplotlib
njsgcs9 小时前
ue4 开放exec接口 vscode mcp铺垫 unreal.register_slate_post_tick_callback
ide·vscode·ue4
肖邦德夜曲9 小时前
SSH连接:mobaXterm(windows)与VSCODE
ide·vscode·ssh
qq_1780570711 小时前
IntelliJ IDEA is not responding报错解决
java·ide·intellij-idea
MS1896377374611 小时前
乾芯课堂系列(二)乾芯DSP开发环境 – QX-IDE安装使用入门
ide·mcu·dsp开发
山上春11 小时前
把 Odoo 日志升级成 IDE 级体验:彩色高亮、可点击源码、统一格式(VS Code)
ide
海棠AI实验室1 天前
第 十五 章 可视化入门:Matplotlib 做出像样的图
matplotlib
熬夜不洗澡1 天前
如何在pycharm中使用Yolo
ide·yolo·pycharm
全干工程师—1 天前
在liunx下使用VScode检测到 #include 错误。请更新 includePath问题的解决方法
ide·vscode·编辑器