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

相关推荐
言之。35 分钟前
Claude Code IDE 集成工作原理详解
ide·人工智能
悠悠子衿121381 小时前
Claude+VSCODE配置git导致频繁弹出git bash 的CMO窗口解决方法
ide·git·vscode
大志哥1232 小时前
IntelliJ IDEA父子工程中导入公网远程备份项目到新目录
java·ide·intellij-idea
梁萌2 小时前
idea使用AI插件(CodeGeeX)
java·ide·ai·intellij-idea·插件·codegeex
wqfhenanxc3 小时前
vscode/cursor 远程Linux基础命令
linux·ide·vscode
Lv11770084 小时前
Visual Studio中的常量和只读变量
ide·笔记·c#·visual studio
Balding Curry4 小时前
vscode快速添加插件的3种方式
ide·vscode·编辑器
yyovoll4 小时前
循环知识点介绍 -蓝桥杯
jvm·ide·java-ee
Clarence Liu5 小时前
MacOS 在Trae IDE中构建现代C++开发环境:从新手到高效的完整指南
c++·ide·macos
木楚5 小时前
在idea中构建传统ssm框架的步骤和方式
java·ide·intellij-idea