shnaz
shnaz

Reputation: 125

Why does Matplotlib savefig images overlap?

I have built a GUI app in python which uses Tkinter.

This app produces and displays an image when a button is clicked.

The image is produced with matplotlib savefig("displayimage.png") in same folder as my app .py file.

It shows the image fine when the button is pressed first time, but when it is pressed second time the new image overlaps the old one.

I tried to remove the existing image from the folder by os.remove("displayimage.png"), but this doesnt help at all.

Do you know why it doesnt just overwrites the old image instead of overlap?

ps. i have tried saving as .jpg but same result.

thanks in advance. Code:

# 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)

Upvotes: 8

Views: 8780

Answers (1)

Misgevolution
Misgevolution

Reputation: 845

its because you didn't clear the buffer. use the the plot.clf() method.and it will be alright.

Upvotes: 25

Related Questions