python runtime_error ai_generated true

UserWarning: FigureCanvasAgg 是非交互式的,因此无法显示

UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown

ID: python/matplotlib-no-display-figure

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-05-02首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

在 matplotlib 使用非交互后端 (Agg) 时调用了 plt.show(),通常发生在无头环境或调用了 matplotlib.use('Agg') 之后。

English

plt.show() called while matplotlib is using a non-interactive backend (Agg), typically on a headless environment or after matplotlib.use('Agg').

generic

解决方案

  1. 97% 成功率
    fig, ax = plt.subplots()
    ax.plot([1,2,3])
    fig.savefig('plot.png', dpi=150)
  2. 82% 成功率
    pip install PyQt5
    import matplotlib
    matplotlib.use('Qt5Agg')
    import matplotlib.pyplot as plt
    plt.plot([1,2,3]); plt.show()

无效尝试

常见但无效的做法:

  1. 90% 失败

    The block argument only controls blocking behavior for interactive backends; Agg still cannot render a window.

  2. 85% 失败

    The backend selection is environment-driven, not an installation defect.