The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
00:45 Accepted answer (Score 136)
00:59 Answer 2 (Score 212)
01:21 Answer 3 (Score 38)
02:04 Answer 4 (Score 14)
02:19 Thank you

--

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

Question links:
[PostScript]: http://en.wikipedia.org/wiki/PostScript

--

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.