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

相关推荐
司悠9 小时前
【解决在vscode里开服务器登录codeX后发消息会一直reconnecting】
服务器·ide·vscode
Allenliu _Andy14 小时前
2026 最新版|免登录适配国内网络 Claude Code 终端版安装配置教程(Windows)
ide·chatgpt·openai·ai编程·claude code
CTA量化套保15 小时前
Jupyter Notebook 反复运行天勤策略内存涨:close 与内核习惯
ide·人工智能·python·jupyter
Esaka_Forever21 小时前
PyCharm 社区版无法识别 .db 文件类型
数据库·ide·pycharm
dust_and_stars1 天前
在Ubuntu 24.04上设置Jupyter Notebook远程访问
linux·ubuntu·jupyter
是上好佳佳佳呀1 天前
【数据分析|Day02】Matplotlib 数据可视化笔记
笔记·matplotlib
ice8130331811 天前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
AD钙奶-lalala2 天前
Android Studio新建项目默认不使用Compose模版
android·ide·android studio
专注VB编程开发20年2 天前
开发VS2026插件最佳方案:老式VSIX EnvDTE
ide·c#·visual studio
专注VB编程开发20年2 天前
VS2026最新ide插件VisualStudio.Extensibility进程外 OOP 新模型
ide·c#·visual studio