如何告诉matplotlib我已经完成了一个绘图?

2024-03-28 18:59:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 文件代码importmatplotlibas绘制pltls
3条回答

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

有一个clear figure命令,它应该为您执行:

plt.clf()

如果在同一图形中有多个子块

plt.cla()

清除当前轴。

例如,可以使用figure创建新的绘图,或者在第一个绘图之后使用close

相关问题 更多 >