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

相关推荐
爱吃泡芙的小白白26 分钟前
如何在现有配置好环境的Pycharm中安装jupyterlab这个工具
ide·python·pycharm·notebook·虚拟环境·jupyterlab
Michael_lcf3 小时前
WebStorm配置与插件
ide·webstorm
Lv11770085 小时前
初识Visual Studio中的 WinForm
开发语言·ide·笔记·c#·visual studio
Qhumaing6 小时前
解决因为jupyter notebook修改路径下没有c.NotebookApp.notebook_dir而无法修改目录问题
ide·python·jupyter
南_山无梅落7 小时前
PyCharm 安装了库却无法 Alt + Enter 导入?(简洁排查版)
ide·python·pycharm·虚拟环境·alt·enter·.venv
invicinble7 小时前
透视IDEA,IDEA认识到什么程度算精通
java·ide·intellij-idea
狂放不羁霸8 小时前
VSCode | 设置保存时自动格式化 Python 文件
ide·vscode·python
hopsky8 小时前
智能编程Cline在vscode 中的使用技巧
ide·vscode·编辑器
IT·陈寒8 小时前
2025年AI开发工具生态总结:从爆款模型到智能IDE的蜕变
ide·人工智能
逍遥_xiaoyao8 小时前
Vscode 配置教程
ide·vscode·编辑器