The Python Oracle

How do I tell matplotlib that I am done with a plot?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping

--

Chapters
00:00 How Do I Tell Matplotlib That I Am Done With A Plot?
00:27 Accepted Answer Score 141
00:38 Answer 2 Score 224
00:52 Answer 3 Score 38
01:24 Answer 4 Score 14
01:33 Thank you

--

Full question
https://stackoverflow.com/questions/7418...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #matplotlib #plot

#avk47



ANSWER 1

Score 224


There is a clear figure command, and it should do it for you:

plt.clf()

If you have multiple subplots in the same figure

plt.cla()

clears the current axes.




ACCEPTED ANSWER

Score 141


You can use figure to create a new plot, for example, or use close after the first plot.




ANSWER 3

Score 38


As stated from @DavidCournapeau, use figure().

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.figure()
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("first.ps")


plt.figure()
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")

Or subplot(121) / subplot(122) for the same plot, different position.

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

plt.subplot(121)
x = [1,10]
y = [30, 1000]
plt.loglog(x, y, basex=10, basey=10, ls="-")

plt.subplot(122)
x = [10,100]
y = [10, 10000]
plt.loglog(x, y, basex=10, basey=10, ls="-")
plt.savefig("second.ps")



ANSWER 4

Score 14


Just enter plt.hold(False) before the first plt.plot, and you can stick to your original code.