我正在创建一个Python模拟模型,模拟重力波中的水粒子。到目前为止我的代码是
import numpy as np
#import matplotlib
import matplotlib.pyplot as plt
#import pylab
#pylab.ion()
h = 2 # water depth
D = 0.4 # wave amplitude
k = 1 # wave number
ds = 0.5
x = np.arange(0, 15+ds, ds)
y = np.arange(0, h+ds, ds)
g = 10
w = np.sqrt(g*k*np.tanh(k*h))
T = 2*np.pi/w
xx,yy = np.meshgrid(x,y)
hp = plt.plot(xx[:],yy[:],'x')
plt.axis('equal')
plt.axis([0,15,0,8]) #axis equal
N = 24
#matplotlib.interactive(True)
t = 0
while (True):
t = t + T/N
dispx = D/np.sinh(k*h) * np.outer(np.cosh(k*y), np.cos(k*x-w*t))
dispy = D/np.sinh(k*h) * np.outer(np.sinh(k*y), np.sin(k*x-w*t))
plt.setp(hp,'XData',xx[:]+dispx[:], 'YData',yy[:]+dispy[:])
plt.drawNow()我只是得到一个静态的图像,我知道我已经接近了。我还应该加些什么才能让所有东西都动起来?最好是实时绘图。
发布于 2014-02-19 04:10:58
您可以使用计时器回调:
import numpy as np
import matplotlib.pyplot as plt
h = 2 # water depth
D = 0.4 # wave amplitude
k = 1 # wave number
ds = 0.5
x = np.arange(0, 15+ds, ds)
y = np.arange(0, h+ds, ds)
g = 10
w = np.sqrt(g*k*np.tanh(k*h))
T = 2*np.pi/w
xx,yy = np.meshgrid(x,y)
hp = plt.plot(xx[:],yy[:],'x')
plt.axis('equal')
plt.axis([0,15,0,8]) #axis equal
N = 24
fig = plt.gcf()
def update():
t = 0
while (True):
t = t + T/N
dispx = D/np.sinh(k*h) * np.outer(np.cosh(k*y), np.cos(k*x-w*t))
dispy = D/np.sinh(k*h) * np.outer(np.sinh(k*y), np.sin(k*x-w*t))
plt.setp(hp,'XData',xx[:]+dispx[:], 'YData',yy[:]+dispy[:])
fig.canvas.draw_idle()
yield
timer = fig.canvas.new_timer(interval=10)
timer.add_callback(update().next)
timer.start()
plt.show()发布于 2014-02-19 04:54:59
我能够在我的笔记本电脑上获得15 fps,只需在导入pyplot并将plt.drawNow()更改为plt.draw()之后添加plt.drawNow()即可。第二步是因为我在pyplot中没有drawNow方法。
https://stackoverflow.com/questions/21869765
复制相似问题