# FileNotFoundError: [Errno 2] 没有那个文件或目录: '/nonexistent/path/figure.png'

- **ID:** `python/matplotlib-plot-directory-error`
- **领域:** python
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试将图形保存到不存在的目录。

## 版本兼容性

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

## 解决方案

1. **Create the directory before saving** (95% 成功率)
   ```
   import os; os.makedirs('/nonexistent/path', exist_ok=True); plt.savefig('/nonexistent/path/figure.png')
   ```
2. **Use a valid existing directory** (90% 成功率)
   ```
   plt.savefig('./figure.png')
   ```

## 无效尝试

- **Using a relative path without checking current working directory** — If the relative path points to a non-existent directory, the error persists. (70% 失败率)
- **Creating the file manually before saving** — The directory must exist; creating the file alone does not create parent directories. (90% 失败率)
