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

相关推荐
on_pluto_10 小时前
【debug】vscode 和 codex 不兼容问题
ide·人工智能·vscode·编辑器
一杯原谅绿茶14 小时前
VSCode的Eclipse插件-编译Eclipse项目
ide·vscode·eclipse
etc95271 天前
keil下如何指定stack和heap地址
ide·mcu
JTaoX1 天前
PyCharm 连接本地虚拟机完整指南
ide·python·pycharm
迷路爸爸1802 天前
cursor资源管理器修改为与vscode一样的纵向布局
ide·vscode·编辑器
星羽XingYu2 天前
esp32 vscode clangd 无法索引库函数解决方案
ide·vscode·编辑器
Dontla2 天前
VSCode Python扩展自动加载.env环境变量机制(Python插件)python.terminal.useEnvFile
ide·vscode·python
平头某2 天前
DSH:Agent 的 VSCode 时刻?
ide·vscode·编辑器
Dontla2 天前
VSCode打开关闭控制台面板快捷键ctrl+`(terminal快捷键、vscode控制台快捷键)
ide·vscode·编辑器
JTaoX2 天前
pycharm 集成本地 jupyter notebook
ide·jupyter·pycharm