Bil*_*ham 5 python interactive mayavi
我想做的是循环更新 mayavi 图。我希望绘图的更新在我指定的时间完成(与动画装饰器不同)。
因此,我想要运行的一段示例代码是:
import time
import numpy as np
from mayavi import mlab
V = np.random.randn(20, 20, 20)
s = mlab.contour3d(V, contours=[0])
for i in range(5):
time.sleep(1) # Here I'll be computing a new V
V = np.random.randn(20, 20, 20)
# Update the plot with the new information
s.mlab_source.set(scalars=V)
Run Code Online (Sandbox Code Playgroud)
但是,这不显示数字。如果我包含mlab.show()在循环中,那么这会窃取焦点并且不允许代码继续。
我觉得我应该使用的是特征图(例如this)。我可以按照示例特征应用程序运行一个图形,该图形在更新滑块时实时更新。但是,当我的代码要求它更新时,我无法让它更新;现在的焦点被“偷走了” visualization.configure_traits()。
任何指示或适当文档的链接将不胜感激。
编辑
大卫·温彻斯特的回答离解决方案又近了一步。
但是,正如我在评论中指出的那样,我无法在该time.sleep()步骤中用鼠标操作该图形。正是在这一步中,在完整的程序中,计算机将忙于计算 V 的新值。在这段时间里,我希望能够操纵图形,用鼠标旋转它等。
如果您使用后端,如果您想在某些长时间运行的函数期间与数据进行交互,wx则可以定期调用。wx.Yield()在下面的示例中,wx.Yield()为某些“长时间运行”函数的每次迭代调用animate_sleep。在这种情况下,您可以使用以下命令启动程序$ ipython --gui=wx <program_name.py>
import time
import numpy as np
from mayavi import mlab
import wx
V = np.random.randn(20, 20, 20)
f = mlab.figure()
s = mlab.contour3d(V, contours=[0])
def animate_sleep(x):
n_steps = int(x / 0.01)
for i in range(n_steps):
time.sleep(0.01)
wx.Yield()
for i in range(5):
animate_sleep(1)
V = np.random.randn(20, 20, 20)
# Update the plot with the new information
s.mlab_source.set(scalars=V)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2886 次 |
| 最近记录: |