python runtime_error ai_generated true

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

ID: python/matplotlib-no-display-figure

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-05-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 85% fail

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