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

相关推荐
棒棒的唐13 小时前
配置 VSCode 的 PHP Intelephense 插件,去掉因php版本不同导至的红色波浪线误判
ide·vscode·php
namas884815 小时前
APLC IDE 用户手册
ide·单片机·嵌入式硬件
2501_9160088917 小时前
Xcode功能、下载、反馈与版本支持详细解析
ide·vscode·macos·ios·个人开发·xcode·敏捷流程
shimly1234561 天前
vscode 很难删除 copilot,如何关闭 copilot 功能?
ide·vscode·copilot
qq_396153452 天前
idea + claude code [bate] 初体验
ide·idea
iCxhust2 天前
8086/8088单板机VSCode集中环境开发编译(第二版整理)
ide·vscode·嵌入式硬件·编辑器·嵌入式·微机原理·8086最小系统
努力的小帅2 天前
使用 Tailscale 实现 Ubuntu 远程开发(VSCode Remote SSH + NoMachine)
linux·ide·vscode·ubuntu·编辑器·copilot
开发者联盟league2 天前
解决vscode默认终端log日志输出中文乱码的问题
ide·vscode·编辑器
2501_916007472 天前
XCode 15 IDE新特性:苹果集成开发环境全面升级,提升编程效率与体验
ide·vscode·macos·ios·个人开发·xcode·敏捷流程
贾亚超2 天前
手动配置vscode-server
ide·vscode·编辑器