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

相关推荐
胡乱编胡乱赢6 小时前
在pycharm终端安装torch
ide·深度学习·pycharm·安装torch
梁小憨憨6 小时前
PyCharm 连接 AutoDL 远程服务器
服务器·ide·pycharm
一骑红尘荔枝来10 小时前
转载:VSCODE 关闭文件和资源管理器关联
ide·vscode·编辑器
float_六七12 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
android_xc15 小时前
Android Studio国内仓库配置
android·ide·android studio
摩羯座-1856903059415 小时前
Python数据可视化基础:使用Matplotlib绘制图表
大数据·python·信息可视化·matplotlib
Teletele-Lin19 小时前
Miniconda安装与VSCode搭建远程Python、Jupyter开发环境
vscode·python·jupyter·环境配置·远程开发
AiTop10019 小时前
腾讯推出AI CLI工具CodeBuddy,国内首家同时支持插件、IDE和CLI三种形态的AI编程工具厂商
ide·人工智能·ai·aigc·ai编程
android_xc1 天前
Android Studio适配butterknife遇到的坑
android·ide·android studio·butterknife
云梦谭1 天前
Cursor 编辑器:面向 AI 编程的新一代 IDE
ide·人工智能·编辑器