mayavi points3d 绘图动画无法更新下一帧

3 投票
1 回答
1697 浏览
提问于 2025-04-18 14:38

我正在写一段代码,用来展示四个图形在空间中移动的效果。目前,mayavi窗口只显示了这些图形的初始位置,但没有更新到下一个位置。

    #Library decleration 
import numpy as np
from mayavi.mlab import *

....

#Inputting the intital positions into the storage vector
storage_position[0].append(v_1.theta)
storage_position[1].append(v_1.phi)
#Calculating the rest of the positions using the symmetry given
storage_position = Sym(storage_position)

#Plotting the intitial positions 

x_coord = x_trans( storage_position)
y_coord = y_trans(storage_position)
z_coord = z_trans( storage_position)


plt = points3d(x_coord, y_coord, z_coord)

msplt = plt.mlab_source
@mlab.animate(delay=100)
def anim(storage_position, storage_vort, no_vort ,x_coord, y_coord, z_coord):
    f = mlab.gcf()
    while True:
    #for i in range(10):      
        #Working out the hamiltonian
        #Hami(storage_position, storage_vort, 1 - 1, no_vort-1)

        transfer_vector = method(storage_position, storage_vort, 1 - 1, no_vort-1)
        storage_position[0].append(transfer_vector[0])
        storage_position[1].append(transfer_vector[1])
        storage_position = Sym(storage_position)


        x_coord = x_trans( storage_position)
        y_coord = y_trans(storage_position)
        z_coord = z_trans( storage_position)

        msplt.set(x_coord = x_coord, y_coord = y_coord, z_coord = z_coord)

        yield


anim(storage_position, storage_vort, no_vort - 1,x_coord, y_coord, z_coord)
mlab.show()

x_coord等是一些numpy向量,用来存储四个图形的x坐标。x_trans等是一些函数,用来在动画的每一步更新这些向量,给它们新的坐标。

1 个回答

4

尝试把这一行替换成:

msplt.set(x_coord = x_coord, y_coord = y_coord, z_coord = z_coord)

用这个:

msplt.set(x=x_coord, y=y_coord, z=z_coord)

点集数据源(应该是一个类型为 MGlyphSource 的对象)知道 xyz 的信息,但 x_coordy_coordz_coord 这些参数不支持作为关键字参数使用。

这里有一个完整的示例,帮助你入门:

from mayavi import mlab
from numpy import array, cos, sin, cos

x_coord = array([0.0, 1.0, 0.0, -1.0])
y_coord = array([1.0, 0.0, -1.0, 0.0])
z_coord = array([0.2, -0.2, 0.2, -0.2])

plt = mlab.points3d(x_coord, y_coord, z_coord)

msplt = plt.mlab_source
@mlab.animate(delay=100)
def anim():
    angle = 0.0
    while True:
        x_coord = array([sin(angle), cos(angle), -sin(angle), -cos(angle)])
        y_coord = array([cos(angle), -sin(angle), -cos(angle), sin(angle)])
        msplt.set(x=x_coord, y=y_coord)
        yield
        angle += 0.1

anim()
mlab.show()

注意,当你运行这个代码时,可能会在控制台看到很多警告信息。别担心,忽略它们就行:这些警告是无害的,属于一个已知问题

撰写回答