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

相关推荐
你说我胖?5 小时前
Visual Studio编辑生成exe
ide·visual studio
cgblpx15 小时前
vscode的安装
ide·vscode·编辑器
烂蜻蜓19 小时前
Matplotlib入门教程(十三):imshow() 详解——灰度图、彩色图、热力图与矩阵可视化
线性代数·矩阵·matplotlib
文祐20 小时前
Windows系统下载pycharm-community-2025.2
ide
小小龙学IT21 小时前
Python Matplotlib 数据可视化库深度解析
python·信息可视化·matplotlib
golang学习记2 天前
VSCode多光标迎来重大更新
ide·vscode·编辑器
53488736abcdefg2 天前
Anaconda+Jupyter+NumPy
jupyter·numpy·anaconda
夜雪一千2 天前
如何取消PyCharm自动识别test开头文件并进入测试模式
ide·python·pycharm
小李不困还能学2 天前
PyCharm远程开发连接失败排查与解决
ide·python·pycharm
测试开发Kevin2 天前
IDEA工程结构解析:项目、模块、库、Facet、Artifact (工件) 概念说明
java·ide·intellij idea