python
runtime_error
ai_generated
true
ValueError: No figure with number 1 exists
ID: python/matplotlib-savefig-no-fignum
80%Fix Rate
85%Confidence
0Evidence
2024-10-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Calling plt.figure(1) or plt.savefig on a figure number that was closed (plt.close('all')) or never created.
generic中文
调用了 plt.figure(1) 或对已关闭(plt.close('all'))或从未创建的图形编号调用 plt.savefig。
Workarounds
-
96% success
fig = plt.figure(num=1, figsize=(6,4)) ax = fig.add_subplot(111) ax.plot([1,2,3]) fig.savefig('out.png') -
97% success
fig, ax = plt.subplots() ax.plot([1,2,3]) fig.savefig('out.png') # never rely on figure numbers
Dead Ends
Common approaches that don't work:
-
85% fail
The figure genuinely does not exist; subsequent savefig calls will still fail.
-
80% fail
draw() only redraws existing figures; it does not create one with a given number.