在使用plt.show()语句时,遇到警告信息:
MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
或遇到:
UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown
或遇到:
UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
plt.show()
因为Agg是一个非GUI的后端,用于生成文件而不是显示窗口。因此,当你使用Agg后端时,plt.show()无法正常工作。
【问题解决】:
在调用 matplotlib 库时,添加一句:matplotlib.use('TkAgg')在代码中使用支持GUI的后端,例如TkAgg
或Qt5Agg
,以便能够调用plt.show()
即:
import matplotlib
matplotlib.use('TkAgg')