动画 Mayavi points3d 图 [英] Animating a mayavi points3d plot

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

问题描述

我正在尝试制作粒子轨迹的视频.然而,不知何故我的场景永远不会更新.这是一个非常简单的例子:

I'm trying to make a video of the trajectories of particles. However, somehow my scene never updates. Here's a very simple example:

from __future__ import absolute_import, division, print_function
from mayavi import mlab
import numpy as np
import math

alpha = np.linspace(0, 2*math.pi, 100)  

xs = np.cos(alpha)
ys = np.sin(alpha)
zs = np.zeros_like(xs)

mlab.points3d(0,0,0)
plt = mlab.points3d(xs[:1], ys[:1], zs[:1])

@mlab.animate(delay=100)
def anim():
    f = mlab.gcf()
    while True:
        for (x, y, z) in zip(xs, ys, zs):
            print('Updating scene...')
            plt.mlab_source.x[0] = x
            plt.mlab_source.y[0] = y
            plt.mlab_source.z[0] = z
            f.scene.render()
            yield


anim()
mlab.show()

如果我运行这个脚本,它会显示一个带有两个点和动画 GUI 的窗口.它还在终端上打印连续的更新场景..."消息流.然而,现场根本没有任何动作.

If I run this script, it shows a window with the two points and the animation GUI. It also prints a continous stream of "Updating Scene..." messages on the terminal. However, the scene doesn't show any movement at all.

我做错了什么?

Python 2.7、Mayavi 4.1、VTK 5.8

Python 2.7, Mayavi 4.1, VTK 5.8

推荐答案

只需更改为:

...

    for (x, y, z) in zip(xs, ys, zs):
        print('Updating scene...')
        plt.mlab_source.set(x=x, y=y, z=z)
        yield

...

根据 documentation mlab_source.set 保证刷新.

you don't even need the f.scene.render(), according to documentation mlab_source.set guarantees the refresh.

此外,由于数据的 shape 不会改变,因此您无需使用 mlab_source.reset.

Also since shape of your data doesn't change you don't need to use mlab_source.reset.

我也测试过并且工作正常.

I also tested and works fine.

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

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