Python 优雅地退出matplotlib.pyplot动画
我有一个脚本,可以绘制一些测光光圈的数据,我想在xy图中绘制它们。我将matplotlib.pyplot与python 2.5一起使用 输入数据存储在大约500个文件中并读取。我知道这不是输入数据的最有效方式,但这是另一个问题 示例代码:Python 优雅地退出matplotlib.pyplot动画,python,animation,quit,matplotlib,Python,Animation,Quit,Matplotlib,我有一个脚本,可以绘制一些测光光圈的数据,我想在xy图中绘制它们。我将matplotlib.pyplot与python 2.5一起使用 输入数据存储在大约500个文件中并读取。我知道这不是输入数据的最有效方式,但这是另一个问题 示例代码: import matplotlib.pyplot as plt xcoords = [] ycoords = [] # lists are populated with data from first file pltline, = plt.plot(x
import matplotlib.pyplot as plt
xcoords = []
ycoords = []
# lists are populated with data from first file
pltline, = plt.plot(xcoords, ycoords, 'rx')
# then loop populating the data from each file
for file in filelist:
xcoords = [...]
ycoords = [...]
pltline.set_xdata(xcoords)
pltline.set_ydata(ycoords)
plt.draw()
当有超过500个文件时,我偶尔会想在绘图中间关闭动画窗口。我的绘图代码可以工作,但它退出得不太优雅。打印窗口对单击关闭按钮没有响应,我必须
Ctrl+C
退出
有谁能帮我找到一种方法,在脚本运行时关闭动画窗口,同时看起来很优雅(比一系列python回溯错误更优雅) 如果您更新数据并在循环中绘制,您应该能够中断它。下面是一个示例(绘制一个固定圆,然后围绕周长移动一条线):
我输入了
time.sleep(.01)
命令,以确保我可以中断运行,但在我的测试(运行Linux)中,这是没有必要的。当我尝试运行您的程序时,绘图会闪烁到屏幕上,程序会引发一个类型错误:NoneType not iterable由于数据数组中有一些none,所以有一个输入错误。我已经修好了,现在应该可以用了。
from pylab import *
import time
data = [] # make the data
for i in range(1000):
a = .01*pi*i+.0007
m = -1./tan(a)
x = arange(-3, 3, .1)
y = m*x
data.append((clip(x+cos(a), -3, 3),clip(y+sin(a), -3, 3)))
for x, y in data: # make a dynamic plot from the data
try:
plotdata.set_data(x, y)
except NameError:
ion()
fig = figure()
plot(cos(arange(0, 2.21*pi, .2)), sin(arange(0, 2.21*pi, .2)))
plotdata = plot(x, y)[0]
xlim(-2, 2)
ylim(-2, 2)
draw()
time.sleep(.01)