# ValueError: dpi必须是正数，但得到了-100

- **ID:** `python/matplotlib-savefig-dpi-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在savefig()或创建图形时，为dpi参数传递了负数或零值。

## 版本兼容性

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

## 解决方案

1. **Use a positive integer for dpi** (95% 成功率)
   ```
   plt.savefig('plot.png', dpi=300)
   ```
2. **Check the variable value before passing** (90% 成功率)
   ```
   dpi_value = 300; if dpi_value > 0: plt.savefig('plot.png', dpi=dpi_value)
   ```

## 无效尝试

- **Using a very large positive DPI like 10000** — While it doesn't raise an error, it may cause memory issues or produce excessively large files. (50% 失败率)
- **Passing dpi as a string like '300'** — dpi must be a number, not a string; this raises a different TypeError. (90% 失败率)
