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 小时前
Matplotlib 第三章 布局格式定方圆
matplotlib
梦游钓鱼4 小时前
vscode通过设置ctrl+鼠标滚轮设置终端字体大小
ide·vscode·编辑器
暮色驶过苍茫4 小时前
VSCode 自动更新问题解决记录
ide·vscode·编辑器
星空7 小时前
vscode开发环境配置
ide·vscode·编辑器
Zww08918 小时前
idea配置注释模板
java·ide·intellij-idea
IOFsmLtzR8 小时前
Visual Studio 中的 Agent:内置 Agent 和自定义 Agent
ide·visual studio
深挖派9 小时前
PyCharm 2026.1 全版本安装配置与全功能环境搭建 (保姆级图文教程)
ide·python·pycharm
不明觉厉二十年9 小时前
pytest+pywinauto+pycharm制作mobaxterm 字符串快捷发送器 Demo
ide·pycharm·pytest
Freak嵌入式1 天前
ESP32 实现在线动态安装库和自动依赖安装-使用uPyPI包管理平台
arm开发·ide·嵌入式·micropython·电子·upypi
偶尔贪玩的骑士1 天前
Jupyter Notebook导出带中文字体PDF
ide·jupyter·pdf