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

相关推荐
hi0_64 小时前
vibe coding 开发软件(一) 模型选择和agent简单使用
ide·个人开发·visual studio
惊讶的猫4 小时前
vscode+conda
ide·vscode·conda
谧小夜4 小时前
Visual Studio Code中实现Go语言自动导包教程
ide·vscode·golang
林_学5 小时前
扔掉了本地 IDE,开发部署只要 3 分钟。
ide
白露与泡影5 小时前
放弃 IntelliJ IDEA,转 VS Code 了。。
java·ide·intellij-idea
TT哇5 小时前
基础的IDEA基本使用,如:debug流程、常用快捷键
java·ide·intellij-idea
Motion_zq5 小时前
vscode使用i18n-ally插件,实现自动翻译(配置百度key)
ide·vscode·机器翻译
爱吃羊的老虎5 小时前
从零开始安装到精通的 Jupyter Notebook 完整教程
ide·python·jupyter
arthur.dy.lee5 小时前
在pycharm中使用powershell7解决conda,ohmyposh报错的问题
ide·pycharm·conda
golang学习记6 小时前
JetBrains 推出革命性产品:AI IDE (AIR) —— AI编程全新范式!
ide·人工智能