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

相关推荐
Y38153266210 小时前
serpbase + Pandas + Jupyter 数据分析实战
jupyter·数据分析·pandas
chouchuang12 小时前
day-044-Matplotlib进阶-高级图表
matplotlib
gongzhxu12 小时前
JetBrains IDEA开发环境搭建
java·ide·intellij-idea
独隅13 小时前
DevEco Code Plan+Build 双 Agent 协同开发实效解析
ide·华为
ITmaster07311 天前
告别 IDE?Android CLI 来了,开发进入 AI Agent 时代
android·ide·人工智能
jiang_changsheng2 天前
详细教程__--PyCharm Terminal--切conda环境详解
ide·pycharm·conda
放学校门口见3 天前
vscode部署stm32开发调试环境
ide·vscode·stm32
独隅3 天前
DevEco Code 在 Windows/MacOS 双系统上的完整使用指南
ide·人工智能·windows·macos·华为·harmonyos
2501_916008894 天前
iOS应用开发工具全面解析:如何选择与优化开发效率
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
梦想的颜色5 天前
2026 VibeCoding 工具链精选|IDE + 大模型成套组合推荐,按场景分级收录
ide·trae·ai 编程·vibecoding·国产海外 ai 编程方案·氛围编程成套配置·副业 ai 开发工具栈