python
runtime_error
ai_generated
true
ValueError: 不存在编号为 1 的图形
ValueError: No figure with number 1 exists
ID: python/matplotlib-savefig-no-fignum
80%修复率
85%置信度
0证据数
2024-10-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
调用了 plt.figure(1) 或对已关闭(plt.close('all'))或从未创建的图形编号调用 plt.savefig。
English
Calling plt.figure(1) or plt.savefig on a figure number that was closed (plt.close('all')) or never created.
解决方案
-
96% 成功率
fig = plt.figure(num=1, figsize=(6,4)) ax = fig.add_subplot(111) ax.plot([1,2,3]) fig.savefig('out.png') -
97% 成功率
fig, ax = plt.subplots() ax.plot([1,2,3]) fig.savefig('out.png') # never rely on figure numbers
无效尝试
常见但无效的做法:
-
85% 失败
The figure genuinely does not exist; subsequent savefig calls will still fail.
-
80% 失败
draw() only redraws existing figures; it does not create one with a given number.