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

相关推荐
克里斯蒂亚诺更新3 小时前
myeclipse的pojie
java·ide·myeclipse
Dshuishui13 小时前
VSCode 环境下编译运行 C++ 项目
c++·ide·vscode
swear0113 小时前
【VSCODE 插件 rust-analyzer 使用】打开文件夹
ide·vscode·rust
ShawnLiaoking13 小时前
Github 克隆 PyCharm,vscode
ide·pycharm·github
C1829818257513 小时前
AI idea 集成claude code插件
java·ide·intellij-idea
IT 行者13 小时前
解决 IntelliJ IDEA 内存占用高的两个优化策略:GPU 渲染与虚拟内存配置
java·ide·intellij-idea·ai编程
Ancelin安心13 小时前
西工大noj(C/C++)100题参考题解及注意事项(2024)
c语言·c++·ide·windows·vscode·算法
李昊哲小课14 小时前
Pandas数据分析 - 第十一章:数据可视化
信息可视化·数据挖掘·数据分析·pandas·matplotlib
禹中一只鱼14 小时前
【IDEA 出现 `IDE error occurred`】
java·ide·spring boot·intellij-idea
golang学习记14 小时前
Zed IDE官宣新招:Git Graph 正式支持!
ide·git