cordelia
cordelia

Reputation: 133

Python: How to first print scatter plot and then delete it

I have a question regarding creating a chart in Python using matplotlib and then deleting it to create a fresh chart. I find that when I create a chart called 'firstscatter' and then put in a print statement to print it to my Python console in Spyder, it prints the chart which is fine. However I want the chart to be deleted after it has been printed so that I can have a fresh 'secondscatter' chart printed.

So all in all what I want to see is the firstscatter chart printed, then deleted, and then the secondscatter chart printed.

How do I amend my code below to see both charts printed after running the code?

Thanks a lot and really appreciate your help.

import numpy as np
from numpy import random
import pandas as pd
from matplotlib import pyplot as plt

x = random.rand(30)
print (x)
y = random.rand(30)
print (y)
z = random.rand(50)
print (z)

firstscatter = plt.scatter(x,y,s = z * 777)
print (firstscatter)

firstscatter.remove()
secondscatter = plt.scatter(x,y,s = z*777, c='Chartreuse')
print (secondscatter)

Upvotes: 0

Views: 353

Answers (1)

Aswin Sivaraman
Aswin Sivaraman

Reputation: 168

When using Matplotlib, you must use plt.show() to show the current figure. In your case:

import numpy as np
from numpy import random
import pandas as pd
from matplotlib import pyplot as plt

x = random.rand(30)
print (x)
y = random.rand(30)
print (y)
z = random.rand(50)
print (z)

plt.scatter(x,y,s = z * 777)
plt.show()

plt.scatter(x,y,s = z*777, c='Chartreuse')
plt.show()

Instead of plt.show(), you can use plt.clf() to clear the current figure.

Upvotes: 2

Related Questions