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

相关推荐
摇滚侠5 小时前
IDEA 定义返回值快捷键
java·ide·intellij-idea
-To be number.wan9 小时前
Python数据分析:Matplotlib 绘图练习
python·数据分析·matplotlib
weixin_660096789 小时前
如何关闭vscode中的copilot生成结束后的消息提醒
ide·vscode·copilot
玄同76510 小时前
Trae国际版与国内版深度测评:AI原生IDE的双生花
ide·人工智能·ai编程·cursor·ai-native·trae
Jackson@ML10 小时前
[Kimi重磅出击!]用Kimi Code智能高效开发Web应用程序指南
ide·python·kimi code
muddjsv12 小时前
科研级置信区间(CI)曲线可视化实战(Matplotlib)
matplotlib
muddjsv14 小时前
Matplotlib 科研级颜色映射(Colormap)可视化实战
matplotlib
猿小猴子14 小时前
主流 AI IDE 之一的 OpenCode 介绍
ide·人工智能·ai·opencode
量子炒饭大师1 天前
【一天一个计算机知识】—— VScode 极速搭建:打造你的全能代码武器库
ide·vscode·编辑器
gagaga....1 天前
解决claude code for vscode对话返回 api error: 403 {“error“:{“type“:“forbidden“,“message“:.....的问题过程
ide·vscode·编辑器