views:

2204

answers:

3

The following code plots two .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 ?

+5  A: 

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

David Cournapeau
Great! Thanks! I find it difficult to find what I need in the current documentation format. The pyplot page is very long, and I was not able to see the entry for close() while scrolling. I was searching for something like clean() clear() or flush().
Stefano Borini
The pyplot tutorial does mention clf() in the "multiple figures" section. Note that if you just create a new plot with figure() without closing the old one with close() (even if you close the GUI window), pyplot retains a reference to your old figure, which may look like a memory leak.
Jouni K. Seppänen
+5  A: 

There is a clear figure command...

plt.clf()

Should do it for you.

Randle Taylor
+3  A: 

As stated from David Cournapeau, 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")
WorldCitizeN