Python 为什么Matplotlib savefig图像重叠?

Python 为什么Matplotlib savefig图像重叠?,python,user-interface,matplotlib,tkinter,Python,User Interface,Matplotlib,Tkinter,我用python构建了一个GUI应用程序,它使用Tkinter 此应用程序在单击按钮时生成并显示图像 该图像是使用matplotlib savefig(“displayimage.png”)在与my app.py文件相同的文件夹中生成的 第一次按下按钮时,图像显示良好,但第二次按下时,新图像与旧图像重叠 我试图通过os.remove(“displayimage.png”)从文件夹中删除现有图像,但这根本没有帮助 你知道为什么它不只是覆盖旧图像而不是重叠吗 另外,我曾尝试将其另存为.jpg,但结果

我用python构建了一个GUI应用程序,它使用Tkinter

此应用程序在单击按钮时生成并显示图像

该图像是使用matplotlib savefig(“displayimage.png”)在与my app.py文件相同的文件夹中生成的

第一次按下按钮时,图像显示良好,但第二次按下时,新图像与旧图像重叠

我试图通过os.remove(“displayimage.png”)从文件夹中删除现有图像,但这根本没有帮助

你知道为什么它不只是覆盖旧图像而不是重叠吗

另外,我曾尝试将其另存为.jpg,但结果相同

提前谢谢。代码:

# make a square figure and axes
figure(1, figsize=(6, 6))
ax = axes([0.1, 0.1, 0.8, 0.8])

# The slices will be ordered and plotted counter-clockwise.
labels = words
fracs = percent
colors = ('yellowgreen', 'gold', 'lightskyblue', 'lightcoral', 'blue', 'yellow', 'cyan', 'pink',
          'purple', 'green', 'magenta', 'orange')

pie(fracs, labels=labels, colors=colors,
    autopct='%.1f%%', shadow=True, startangle=90)

title("Most used words", fontsize=20)

savefig('senalyzed_piechart.png',dpi=80)

这是因为你没有清除缓冲区。使用plot.clf()方法。这样就可以了。

请提供您的代码。在绘图前后是否使用pyplot.figure()和pyplot.close()命令?@leeladam我现在添加了代码。我只是用了savefig…就是这样!非常感谢你!下面是一个关于如何清除matplotlib轴和图形的好方法。