Python中Matplotlib在IDE绘图时报MatplotlibDeprecationWarning错误信息

在IDEA或Pycharm中执行Matplotlib报如下错误:

MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.
  fig, ax = plt.subplots()

解决方案有两种。

方案一:代码中指定backend

在代码中指定backend,这样绘制出来的图像便不会在IDE中进行展示。

相关代码如下:

import matplotlib
import matplotlib.pyplot as plt

# 解决绘图异常问题
matplotlib.use('Qt5Agg')

这里backend使用了”Qt5Agg“,绘制时直接弹窗显示。

方案二:设置IDE中的Python设置

设置IDE中的Python设置,取消”Show plots in tool window“。此时,执行会话之后,也不会再在IDE集成的plots中显示,而是弹窗显示,同样解决了上述警告。
在这里插入图片描述
无论是哪个IDE,找到类似的选择,取消勾选即可。

Supongo que te gusta

Origin blog.csdn.net/wo541075754/article/details/131223431
Recomendado
Clasificación