2017-11-25 191 views
-1

我正在編寫一個函數,它可以在3D中動畫隨機遊走,但不幸的是代碼無法正常工作。情節在哪裏,沒有錯誤發生,但沒有發生。我正在使用%matplotlib tk
有我的代碼:3D動畫隨機遊走[Python]

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)開始隨機遊走。我想知道爲什麼。

+0

我已經評論說,你需要返回動畫參考你的最後一個問題如下。你會不會永遠問「隨機遊走」問題? – ImportanceOfBeingErnest

+0

我知道我一直在問這個問題一段時間,我真的很抱歉。自從上一次以來,我取得了很大進步:)不幸的是,我不知道如何返回對動畫的引用 – Hendrra

回答

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() 
+0

非常感謝!現在我完成了。再一次 - 我真的很抱歉,但這是我第一次嘗試動畫劇情。 – Hendrra