还是不行:/home/huxin/bev/bevfusion/main3.py:45: UserWarning: Glyph 25991 (\N{CJK UNIFIED IDEOGRAPH-6587}) missing from current font. plt.savefig('test.png') findfont: Font family ['sans-serif'] not found. Falling back to DejaVu Sans. findfont: Generic family 'sans-serif' not found because none of the following families were found: SimHei
时间: 2025-07-29 14:00:20 浏览: 3
<think>我们面对的问题是:Matplotlib在保存图像时出现中文字体缺失警告(如`UserWarning: Glyph 26512 (\N{CJK UNIFIED IDEOGRAPH-6790}) missing from current font`)并导致中文无法显示。根据引用[1][2][3][4]的信息,根本原因是当前字体不支持中文字符。以下是系统化的解决方案:
### 解决方案步骤
#### 1. **安装中文字体**
- **Linux**:安装文泉驿字体(如引用[3]提到):
```bash
sudo apt-get install fonts-wqy-zenhei # 文泉驿正黑
sudo apt-get install fonts-wqy-microhei # 文泉驿微米黑
```
- **Windows**:下载并安装SimHei(黑体)字体[^4]:
1. 下载`SimHei.ttf`(可从网络获取)
2. 右键选择“安装”或复制到`C:\Windows\Fonts`
- **Mac**:
```bash
brew install wqy-zenhei # 通过Homebrew安装
```
#### 2. **配置Matplotlib使用中文字体**
在Python代码中设置全局字体(引用[3][4]方法):
```python
import matplotlib.pyplot as plt
import matplotlib as mpl
# 方法1:直接指定字体名称(适用于已安装的系统字体)
plt.rcParams['font.sans-serif'] = ['SimHei', 'WenQuanYi Zen Hei', 'sans-serif'] # 中文优先
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
# 方法2:通过字体路径加载(适用于自定义字体位置)
font_path = '/path/to/SimHei.ttf' # 替换为实际路径
font_prop = mpl.font_manager.FontProperties(fname=font_path)
plt.rcParams['font.family'] = font_prop.get_name()
```
#### 3. **清除Matplotlib缓存**
字体更改后必须清除缓存(引用[3]隐含要求):
```bash
rm -rf ~/.cache/matplotlib # Linux/Mac
del %USERPROFILE%\.matplotlib\fontlist-*.json # Windows
```
#### 4. **验证字体配置**
```python
from matplotlib.font_manager import fontManager
# 打印支持中文的字体
print([f.name for f in fontManager.ttflist if 'hei' in f.name.lower() or 'zen' in f.name.lower()])
# 测试绘图
plt.plot([1,2], [3,4])
plt.title('中文测试')
plt.savefig('test.png', dpi=300) # 提高DPI避免字形模糊
plt.show()
```
#### 5. **保存图像时指定高DPI**
原始警告中`plt.savefig('autocorrelation.png', dpi=120)`的DPI较低,可能导致字形丢失。建议:
```python
plt.savefig('output.png', dpi=300, bbox_inches='tight') # dpi≥300可确保清晰
```
### 常见错误排查
- **字体名称错误**:使用`mpl.font_manager.findfont('SimHei')`验证字体是否注册。
- **环境隔离**:Docker/虚拟环境中需在容器内安装字体(见引用[3]的扩展)。
- **Mac特殊处理**:禁用LaTeX引擎(引用[3]):
```python
mpl.rc('text', usetex=False) # 避免与LaTeX冲突
```
> 引用说明:本方案综合了多平台字体安装方法[^3][^4],并通过缓存清除和DPI调整确保可靠性[^1][^2]。
---
### §§相关问题§§
1. 如何在Matplotlib中同时显示中文和LaTeX公式?
2. Matplotlib保存图像时如何避免边缘内容被裁剪?
3. 在Docker容器中运行Matplotlib时如何解决字体缺失问题?
4. 除了SimHei,还有哪些适合科研绘图的中文字体?</think>### 解决 Matplotlib 保存图像时中文字体缺失问题
当使用 Matplotlib 的 `savefig()` 函数保存包含中文的图像时出现 `UserWarning: Glyph CJK UNIFIED IDEOGRAPH missing from current font` 警告,这通常是由于系统缺少中文字体支持或 Matplotlib 未正确配置中文显示造成的。以下是完整解决方案:
#### 1. **安装中文字体(推荐安装文泉驿字体)**
```bash
# Linux 系统安装
sudo apt-get install fonts-wqy-microhei # 文泉驿微米黑
sudo apt-get install fonts-wqy-zenhei # 文泉驿正黑
# Windows 系统
# 1. 下载 SimHei.ttf 字体文件
# 2. 右键选择"安装"或复制到 C:\Windows\Fonts 目录
```
#### 2. **清除 Matplotlib 字体缓存**
```bash
# 查找缓存目录
python -c "import matplotlib as mpl; print(mpl.get_cachedir())"
# 删除缓存文件(示例路径)
rm -rf ~/.cache/matplotlib/* # Linux/Mac
del C:\Users\<用户名>\.matplotlib\fontlist-*.json # Windows
```
#### 3. **代码中配置中文字体**
```python
import matplotlib.pyplot as plt
import matplotlib as mpl
# ✅ 推荐方案:全局配置中文显示
mpl.rcParams['font.sans-serif'] = ['WenQuanYi Micro Hei', 'SimHei', 'Microsoft YaHei'] # 字体优先级列表
mpl.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
# 替代方案:指定字体路径(适用于自定义字体位置)
font_path = '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc' # Linux 路径示例
# font_path = 'C:/Windows/Fonts/simhei.ttf' # Windows 路径示例
font_prop = mpl.font_manager.FontProperties(fname=font_path)
plt.title('中文标题', fontproperties=font_prop)
```
#### 4. **保存图像时提高分辨率**
```python
# 保存时设置高DPI(至少300)和边界控制
plt.savefig('output.png',
dpi=300,
bbox_inches='tight', # 自动裁剪空白边缘
pad_inches=0.1) # 保留10%的边距
```
#### 5. **验证字体配置**
```python
# 检查可用中文字体
from matplotlib.font_manager import fontManager
chinese_fonts = [f for f in fontManager.ttflist if any('CJK' in x for x in f.name)]
print("可用中文字体:", [f.name for f in chinese_fonts])
# 测试中文显示
plt.figure()
plt.plot([1,2], [3,4])
plt.title('中文测试 - 北京')
plt.savefig('test.png', dpi=300)
```
#### 常见问题排查表
| 问题现象 | 解决方案 |
|---------|---------|
| 保存后中文显示为方块 | 1. 确认字体安装成功<br>2. 清除字体缓存<br>3. 检查字体名称拼写 |
| Windows 安装字体后仍无效 | 1. 重启Python环境<br>2. 检查字体路径是否正确<br>3. 尝试使用绝对路径 |
| Docker 容器内问题 | 在Dockerfile中添加:<br>`RUN apt-get update && apt-get install -y fonts-wqy-zenhei` |
| 部分中文显示缺失 | 1. 更换字体(如改用Microsoft YaHei)<br>2. 增加DPI至400-600 |
> 引用说明:解决方案参考了多平台下Matplotlib中文字体缺失问题的处理经验[^3][^4],特别针对`savefig`时的字体渲染优化进行了适配[^1][^2]。
---
###
阅读全文
相关推荐




















