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

相关推荐
外收内放21 小时前
数据分析(图表信息详解)
python·学习·数据分析·matplotlib
丑大王21 小时前
ultralytics 环境安装与正确使用Vscode
ide·yolo·编辑器
lhh_qrsly1 天前
TI CCS(Code Composer Studio,德州仪器嵌入式 IDE)2015~2026 版本总览
ide·php·composer
1024奇点2 天前
【仓颉语言入门 · 第二篇 第 8 课】
开发语言·数据库·ide·mysql·开源
xiaoye-duck2 天前
《C++ 项目:手写大模型接入 SDK》Trae IDE 开发环境搭建与依赖管理
c++·ide·语言模型
一只笨菜鸟2 天前
IDEA如何让多个项目同时显示
java·ide·intellij-idea
哎写bug的程序员3 天前
关于VSCode上gcc,gdb的安装配置问题
ide·vscode·编辑器
程序员清风3 天前
Python 数据可视化入门:Matplotlib 核心用法
python·信息可视化·matplotlib
旺旺的碎冰冰~3 天前
VScode解决终端输出中文乱码问题
ide·vscode·编辑器
devilnumber4 天前
避坑_IDEA 提取方法(Extract Method)工具
java·ide·intellij-idea