下面的代码绘制了两个PostScript (.ps)文件,但第二个文件包含了这两行。

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")

如何告诉matplotlib为第二个情节重新开始?


当前回答

正如@DavidCournapeau所说,使用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")

或subplot(121) / subplot(122)表示同一地块,不同位置。

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")

其他回答

正如@DavidCournapeau所说,使用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")

或subplot(121) / subplot(122)表示同一地块,不同位置。

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")

有一个清晰的数字命令,它应该为你做:

plt.clf()

如果你在同一个图中有多个子图

plt.cla()

清除当前轴。

在第一个plt之前输入plt.hold(False)。绘图,你可以坚持你的原始代码。

如果你正在交互式地使用Matplotlib,例如在一个web应用程序中(例如ipython),你可能正在寻找

plt.show()

而不是pl .close()或plt.clf()。

来自matplotlib的源代码。Pyplot,在figure()文档下:

如果要创建许多图形,请确保显式调用 “.pyplot。关闭你不使用的数字,因为这将 启用pyplot以正确地清理内存。

因此,正如其他人所说,当您完成每个图形时,请在它上使用plt.close(),然后您就可以开始了!

注意:如果你通过f = plt.figure()创建图形,你可以通过plt关闭它。Close (f)而不是f.s Close()。