python runtime_error ai_generated true

ValueError: 不存在编号为 1 的图形

ValueError: No figure with number 1 exists

ID: python/matplotlib-savefig-no-fignum

其他格式: JSON · Markdown 中文 · English
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.

generic

解决方案

  1. 96% 成功率
    fig = plt.figure(num=1, figsize=(6,4))
    ax = fig.add_subplot(111)
    ax.plot([1,2,3])
    fig.savefig('out.png')
  2. 97% 成功率
    fig, ax = plt.subplots()
    ax.plot([1,2,3])
    fig.savefig('out.png')  # never rely on figure numbers

无效尝试

常见但无效的做法:

  1. 85% 失败

    The figure genuinely does not exist; subsequent savefig calls will still fail.

  2. 80% 失败

    draw() only redraws existing figures; it does not create one with a given number.