python
runtime_error
ai_generated
true
ValueError: dpi必须是正数,但得到了-100
ValueError: dpi must be a positive number, got -100
ID: python/matplotlib-savefig-dpi-error
80%修复率
86%置信度
0证据数
2024-03-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.7 | active | — | — | — |
| 3.8 | active | — | — | — |
根因分析
在savefig()或创建图形时,为dpi参数传递了负数或零值。
English
Passing a negative or zero value for the dpi parameter in savefig() or figure creation.
解决方案
-
95% 成功率 Use a positive integer for dpi
plt.savefig('plot.png', dpi=300) -
90% 成功率 Check the variable value before passing
dpi_value = 300; if dpi_value > 0: plt.savefig('plot.png', dpi=dpi_value)
无效尝试
常见但无效的做法:
-
Using a very large positive DPI like 10000
50% 失败
While it doesn't raise an error, it may cause memory issues or produce excessively large files.
-
Passing dpi as a string like '300'
90% 失败
dpi must be a number, not a string; this raises a different TypeError.