python
runtime_error
ai_generated
true
ValueError: dpi must be a positive number, got -100
ID: python/matplotlib-savefig-dpi-error
80%Fix Rate
86%Confidence
0Evidence
2024-03-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.7 | active | — | — | — |
| 3.8 | active | — | — | — |
Root Cause
Passing a negative or zero value for the dpi parameter in savefig() or figure creation.
generic中文
在savefig()或创建图形时,为dpi参数传递了负数或零值。
Workarounds
-
95% success Use a positive integer for dpi
plt.savefig('plot.png', dpi=300) -
90% success Check the variable value before passing
dpi_value = 300; if dpi_value > 0: plt.savefig('plot.png', dpi=dpi_value)
Dead Ends
Common approaches that don't work:
-
Using a very large positive DPI like 10000
50% fail
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% fail
dpi must be a number, not a string; this raises a different TypeError.