Python:在循环中连续添加到3d场景中

2024-04-20 11:12:56 发布

您现在位置:Python中文网/ 问答频道 /正文

你能给我举个例子吗

  1. 最初绘制曲面
  2. 然后在一个循环中画一些随机线。在画每一条线之间,浪费一点时间,给人一种动画的感觉。在

代码应该同时从ipythonpydev工作。在


Tags: 代码ipython时间绘制浪费动画例子曲面
1条回答
网友
1楼 · 发布于 2024-04-20 11:12:56

使用mayavi的一个答案如下:

import numpy as np
from mayavi import mlab
import time
from tvtk.tools import visual


# # prepare surface data
rng = 20
step = 1
X = np.arange(0, rng, step)
Y = X
Z = np.random.uniform(-1, 0, (rng, rng))

# # draw the surface
fig = mlab.figure(size=(500, 500), bgcolor=(1, 1, 1))
visual.set_viewer(fig)
s = mlab.surf(X, Y, Z)
mlab.axes(color=(0, 0, 0))
mlab.view(40, 40)
mlab.outline()

for i in xrange(5):
    # # sleep a little to give the impression of animation
    time.sleep(1)
    # # get coordinates of two random points
    p1 = np.random.uniform(0, 10, (3,)) 
    p2 = np.random.uniform(0, 10, (3,))
    line = np.vstack((p1, p2))
    x, y, z = (line[:, dim] for dim in xrange(3))

    # # connect points
    mlab.plot3d(x, y, z, figure=fig, tube_radius=.05, colormap='Greens')

mlab.show()

相关问题 更多 >