# ImportError: 无法加载需要'tkinter'交互框架的后端'TkAgg'，因为'headless'未运行显示。

- **ID:** `python/matplotlib-agg-backend-missing`
- **领域:** python
- **类别:** module_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在无显示服务器上运行matplotlib，但默认后端(TkAgg)需要图形环境。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.7 | active | — | — |
| 3.8 | active | — | — |

## 解决方案

1. **Set the Agg backend before importing matplotlib.pyplot** (95% 成功率)
   ```
   import matplotlib; matplotlib.use('Agg'); import matplotlib.pyplot as plt
   ```
2. **Use the 'pdf' or 'svg' backend for file output** (90% 成功率)
   ```
   import matplotlib; matplotlib.use('PDF'); import matplotlib.pyplot as plt
   ```

## 无效尝试

- **Installing tkinter via pip** — tkinter is part of the standard library and cannot be installed via pip; the issue is the missing display, not the module. (95% 失败率)
- **Setting backend after importing pyplot** — The backend must be set before importing pyplot; setting it after has no effect. (90% 失败率)
