我怎么告诉matplotlib我已经完成了一个情节?

时间:2009-04-12 14:40:56

标签: python matplotlib plot

以下代码绘制了两个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重新开始第二个情节?

5 个答案:

答案 0 :(得分:148)

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

plt.clf()

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

plt.cla()

清除当前轴。

答案 1 :(得分:110)

例如,您可以使用figure创建新地图,或在第一个地图后使用close

答案 2 :(得分:28)

如David Cournapeau所述,请使用数字()。

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

或子图(121)/子图(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")

答案 3 :(得分:13)

只需在第一个plt.plot之前输入plt.hold(False),就可以坚持原始代码。

答案 4 :(得分:0)

如果它们都不起作用,那么检查一下..如果沿各自的轴有x和y数据数组。然后检查你初始化x和y的哪个单元格(jupyter)为空。这是因为,您可能正在将数据附加到x和y而不重新初始化它们。所以情节也有旧数据。所以检查..

相关问题