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

相关推荐
你的牧游哥1 小时前
Cursor IDE Rules / Skills / Subagents 前端项目配置全指南
前端·ide
星星也在雾里2 小时前
Anaconda命令行配置Jupyter Notebook虚拟环境
python·jupyter
jian110582 小时前
Android studio会自动下载高版本的tools
android·ide·android studio
北风toto3 小时前
vscode离线安装插件,下载好的插件在内网安装,解决内网不能联网无法安装插件的问题
ide·vscode·编辑器
Elastic 中国社区官方博客3 小时前
用于 IntelliJ IDEA 的新 ES|QL 插件
java·大数据·数据库·ide·elasticsearch·搜索引擎·intellij-idea
被放养的研究生3 小时前
vscode-settings.json
ide·vscode·json
白露与泡影3 小时前
为什么在IDEA使用@Autowired会报黄?
java·ide·intellij-idea
黑不溜秋的4 小时前
C++ vscode 常用插件
ide·vscode·编辑器
l1t5 小时前
uv安装的cpython使用matplotlib库作图不能显示的问题起因和解决
matplotlib·uv
likerhood1 天前
IDEA中创建和运行java项目的常见操作
java·ide·intellij-idea