问题
在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.
与此同时,画图要么不弹出图像,要么图像很小挤在右侧窗口,不能很好的展示出来。
代码警告且显示图像不全
解决方案
在代码开头添加mpl.use('TkAgg'),结束时添加plt.show(),就可以不再发出警告,并且图像窗口会弹出显示。
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use('TkAgg')
...
plt.show()
也可以将mpl.use('TkAgg')用plt.switch_backend('TkAgg')替换,效果是一样的,即:
import matplotlib.pyplot as plt
plt.switch_backend('TkAgg')
...
plt.show()
‘TkAgg’是matplotlib的其中一个backend的选项,常见的backend有:Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG。
1. Agg:使用反走样技术生成PNG图像。
2. GTK3Agg:使用GTK3库在GTK3应用程序中渲染绘图。
3. QtAgg:使用Qt库在Qt应用程序中渲染绘图。
4. WXAgg:使用wxPython库在wxWidgets应用程序中渲染绘图。
5. MacOSX:在Mac OS X系统中使用内置的渲染器渲染绘图。
6. WebAgg:将绘图保存为HTML页面,可以在Web浏览器中查看。
7. SVG:将绘图保存为SVG矢量图像。
TkAgg:
WebAgg:
注意使用mpl.use('Agg')是不能直接弹出图片的。建议换成其他backend。