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

相关推荐
sunshine22 girl8 小时前
Idea中如何搜索
java·ide·intellij-idea
kyrie_sakura11 小时前
python学习笔记15 -- Anaconda,Jupyter,numpy
python·学习·jupyter·numpy
一木 之林12 小时前
Miniconda 安装与 Jupyter 使用入门
ide·python·jupyter
辰辉创聚15 小时前
禽流感病毒分子基础:表面抗原与内部转录复合体的功能解析
ide·leetcode·rabbitmq·重组血凝素蛋白·甲型流感病毒核蛋白·禽流感单抗
夏幻灵18 小时前
DeepSeek Harness 上手教程:从 API 密钥到本地 WebUI 完整部署
ide
小葱炖豆腐1 天前
python绘制excel折线图
python·excel·numpy·pandas·matplotlib
星空2 天前
pycharm复习
ide·python·pycharm
七灵微2 天前
【AI】代码实战- 基于时间序列的轴承检测1
人工智能·机器学习·ai·pandas·matplotlib
captain3762 天前
网络原理(3)-TCP核心机制连接管理▲▲▲
java·服务器·网络·ide·网络协议·tcp/ip·java-ee
颜颜yan_2 天前
Eclipse Theia 鸿蒙 PC 适配全记录:在 HarmonyOS PC 上运行完整 IDE 工作台
ide·eclipse·harmonyos