【问题标题】:Animated random walk in 3D [Python]3D 动画随机游走 [Python]
【发布时间】:2017-11-25 21:44:13
【问题描述】:

我正在编写一个函数,该函数将在 3D 中为随机游走设置动画,但不幸的是,该代码无法正常工作。情节在哪里,没有错误发生,但什么也没有发生。我正在使用%matplotlib tk
有我的代码:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation

def path_generator(steps, step):
    path = np.empty((3, steps))
    for i in range(1, steps):
        x_ran, y_ran, z_ran = np.random.rand(3)
        sgnX = (x_ran - 0.5)/abs(x_ran - 0.5)
        sgnY = (y_ran - 0.5)/abs(y_ran - 0.5)
        sgnZ = (z_ran - 0.5)/abs(z_ran - 0.5)
        dis = np.array([step*sgnX, step*sgnY, step*sgnZ])
        path[:, i] = path[:, i - 1] + dis

    return path

def animate(i):
    global particles, trajectories
    for trajectory, particle in zip(trajectories, particles):

    trajectory.set_data(particle[0:2, :i])

    trajectory.set_3d_properties(particle[2, :i])  

return trajectories

def random_walk_3D_animated(n, traj = 1):

    fig = plt.figure()
    ax = p3.Axes3D(fig)

    particles = [path_generator(n, 1) for i in range(traj)]

    trajectories = [ax.plot(particle[0, 0:1], particle[1, 0:1], particle[2, 
                    0:1])[0] for particle in particles]

    ax.set_xlim3d([-100, 100])
    ax.set_ylim3d([-100, 100])
    ax.set_zlim3d([-100, 100])

    animacion = animation.FuncAnimation(fig, animate, 1000, interval=50, 
                                        blit=False)

    plt.show()

奇怪的是,当没有函数 random_walk_3D_animated(n, traj = 1) 并且给出了值 ntraj 时,代码确实有效。有时代码不会从(0,0,0) 开始随机游走。我想知道为什么。

【问题讨论】:

  • 我已经在您的最后一个问题下方评论说您需要返回对动画的引用。你现在会永远继续问“随机游走”的问题吗?
  • 我知道我问这个问题已经有一段时间了,对此我感到非常抱歉。然而,自上一个以来我取得了很大的进步 :) 不幸的是我不知道如何返回对动画的引用

标签: python animation matplotlib


【解决方案1】:
  1. 起始位置将是 emty 数组的内容。这可能是任何值,所以它在这里不是很有用。而是用零初始化 path
  2. 您需要返回对动画的引用。来自animation documentation:“[..] 保持对实例对象的引用至关重要。”

完整示例:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation

def path_generator(steps, step):
    path = np.zeros((3, steps))
    for i in range(1, steps):
        x_ran, y_ran, z_ran = np.random.rand(3)
        sgnX = (x_ran - 0.5)/abs(x_ran - 0.5)
        sgnY = (y_ran - 0.5)/abs(y_ran - 0.5)
        sgnZ = (z_ran - 0.5)/abs(z_ran - 0.5)
        dis = np.array([step*sgnX, step*sgnY, step*sgnZ])
        path[:, i] = path[:, i - 1] + dis

    return path

def animate(i):
    global particles, trajectories
    for trajectory, particle in zip(trajectories, particles):
        trajectory.set_data(particle[0:2, :i])
        trajectory.set_3d_properties(particle[2, :i])  

def random_walk_3D_animated(n, traj = 1):
    global particles, trajectories
    fig = plt.figure()
    ax = p3.Axes3D(fig)

    particles = [path_generator(n, 1) for i in range(traj)]
    trajectories = [ax.plot(particle[0, 0:1], particle[1, 0:1], particle[2, 
                    0:1])[0] for particle in particles]
    ax.set_xlim3d([-100, 100])
    ax.set_ylim3d([-100, 100])
    ax.set_zlim3d([-100, 100])

    animacion = animation.FuncAnimation(fig, animate, 1000, interval=50, 
                                        blit=False)
    return animacion

ani = random_walk_3D_animated(100, traj = 1)
plt.show()

【讨论】:

  • 非常感谢!现在我完成了。再一次 - 我真的很抱歉,但这是我第一次尝试为情节设置动画。
猜你喜欢
  • 2021-10-21
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多