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

相关推荐
醉舞经阁半卷书15 小时前
Matplotlib从入门到精通
python·数据分析·matplotlib
10km7 小时前
MCP:Trace IDE 正确配置 PDF 读取 MCP 的完整指南
ide·pdf·mcp·trae
羊仔AI探索12 小时前
AI心理学导师测评,智能体商单案例
ide·人工智能·ai·aigc
技道两进1 天前
trae+Skills初步实践
ide·trae·skills·skills实践
lucky九年1 天前
Treasure vscode插件
ide·vscode·编辑器
葱明撅腚1 天前
seaborn绘图(下)
python·matplotlib·可视化·seaborn·图表绘制
007php0071 天前
mac笔记本中在PHP中调用Java JAR包的指南
java·ide·python·面试·职场和发展·pycharm·php
可以吧可以吧1 天前
idea全家桶【常见报错处理】当出现 “We could not validate your license ... “ 提示时
java·ide·intellij-idea
装不满的克莱因瓶1 天前
IDEA rebuild project 到底有什么作用?
java·ide·intellij-idea
LXY_BUAA1 天前
《C++学习》_使用visual studio封装程序_20260124
ide·学习·visual studio