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

相关推荐
L-影2 小时前
vscode安装SQLAlchemy步骤
ide·vscode·编辑器
qq_283720052 小时前
Python3 模块精讲:Matplotlib—— 数据可视化、绘图从零基础到实战精通
信息可视化·matplotlib
西门大盗3 小时前
pycharm自动进行python 测试(python test)
ide·python·pycharm
2401_827499995 小时前
数据分析学习06(黑马)-Matplotlib
学习·数据分析·matplotlib
倾听一世,繁花盛开5 小时前
Java语言程序设计——篇十三(1)
java·开发语言·ide·eclipse
Uncertainty!!8 小时前
claude code在pycharm中的安装使用
ide·python·pycharm·claude code
Hy行者勇哥8 小时前
国内外与vscode类似的软件有哪些?国内那些软件可以平替?
ide·vscode·编辑器
写代码的小阿帆9 小时前
AI工具使用——外挂AI插件、AI原生IDE与AI终端
ide·人工智能·ai-native
此生只爱蛋1 天前
【vscode环境配置心得】C++版
c++·ide·vscode
xinhuanjieyi1 天前
windows安装vscode服务端
ide·windows·vscode