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


Question

The following code plots to two PostScript (.ps) files, but the second one contains both lines.

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

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


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

How can I tell matplotlib to start afresh for the second plot?

1
143
3/19/2014 3:03:56 AM

Accepted Answer

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

113
2/7/2012 12:04:34 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon