Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 842
shnaz Avatar asked Dec 03 '13 13:12

shnaz


People also ask

How to avoid overplotting in Python?

Faceting. Faceting is one of the methods you can use to avoid overlapping. Seaborn has a function FacetGrid() for faceting. Note that you can see this post for more information.

How do I make Matplotlib high resolution images?

We can set the dpi value to get a high-quality image. Using the saving() method, we can save the image with format=”png” and dpi=1200. To show the image, use the plt. show() method.

How do I save a specific figure in Matplotlib?

Saving a plot on your disk as an image file Now if you want to save matplotlib figures as image files programmatically, then all you need is matplotlib. pyplot. savefig() function. Simply pass the desired filename (and even location) and the figure will be stored on your disk.


1 Answers

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

like image 99
Misgevolution Avatar answered Sep 18 '22 08:09

Misgevolution