循环更新Mayavi图 [英] Update mayavi plot in loop

查看:175
本文介绍了循环更新Mayavi图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是循环更新mayavi图.我希望在我指定的时间完成图的更新(与例如动画装饰器不同).

What I want to do is to update a mayavi plot in a loop. I want the updating of the plot to be done at a time specified by me (unlike, e.g., the animation decorator).

所以我想开始运行的示例代码是:

So an example piece of code I would like to get running is:

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)

但是,这不会显示数字.如果我在循环中包含mlab.show(),则这会夺取焦点,并且不允许代码继续.

However, this doesn't display a figure. If I include mlab.show() in the loop, then this steals the focus and doesn't allow the code to continue.

我觉得我应该使用的是特征图(例如,

I feel what I should be using is a traits figure (e.g. this). I can follow the example traits application to run a figure which live-updates as I update the sliders. However, I can't get it to update when my code asks it to update; the focus now is 'stolen' by visualization.configure_traits().

任何指针或指向适当文档的链接,将不胜感激.

Any pointers, or a link to appropriate documentation, would be appreciated.

编辑

David Winchester的答案离解决方案更近了一步.

David Winchester's answer gets a step closer to the solution.

但是,正如我在评论中指出的那样,在time.sleep()步骤中,我无法用鼠标来操纵图形.正是在此步骤中,在完整程序中,计算机将忙于计算V的新值.在这段时间内,我希望能够操纵图形,用鼠标等旋转图形.

However, as I point out in the comments, I am not able to manipulate the figure with the mouse during the time.sleep() step. It is during this step that, in the full program, the computer will be busy computing the new value of V. During this time I would like to be able to manipulate the figure, rotating it with the mouse etc.

推荐答案

如果使用wx后端,则要在某些长时间运行的函数中与数据进行交互,可以定期调用wx.Yield().在以下示例中,对于某些长时间运行"函数animate_sleep的每次迭代,均调用wx.Yield().在这种情况下,您可以使用$ ipython --gui=wx <program_name.py>

If you use the wx backend, you can call wx.Yield() periodically if you want to interact with your data during some long-running function. In the following example, wx.Yield() is called for every iteration of some "long running" function, animate_sleep. In this case, you could start the program with $ 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)

这篇关于循环更新Mayavi图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆