【问题标题】:Adding an animation to a random walk plot [Python]将动画添加到随机游走图 [Python]
【发布时间】:2018-05-06 19:45:17
【问题描述】:

我编写了一个绘制随机游走的代码。生成了traj 不同的随机游走,每个随机游走由n 步骤组成。我想动画他们的动作。我该怎么做?
我的代码如下:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def random_walk_2D(n, traj = 1):

    for i in range(traj):

        skoki = np.array([[0, 1], [1, 0], [-1, 0], [0, -1]])
        losy = np.random.randint(4, size = n)

        temp = skoki[losy, :]

        x = np.array([[0, 0]])

        temp1 = np.concatenate((x, temp), axis = 0)

        traj = np.cumsum(temp1, axis = 0)

        plt.plot(traj[:, 0], traj[:, 1])
        plt.plot(traj[-1][0], traj[-1][1], 'ro') #the last point

    plt.show()

【问题讨论】:

    标签: python numpy animation matplotlib random


    【解决方案1】:

    就目前而言,您可以一次性生成轨迹。我的意思是traj = np.cumsum(temp1, axis = 0) 中的traj 已经包含了从头到尾的所有“故事”。如果你想创建一个“实时”的动画,你不应该一次生成traj,而是迭代地绘制新的步骤。怎么办:

    import numpy as np
    import matplotlib.pyplot as plt
    
    def real_time_random_walk_2D_NT(
        nb_steps, nb_trajs, with_dots=False, save_trajs=False, tpause=.01
        ):
        """
        Parameters
        ----------
        nb_steps     : integer
                       number of steps
        nb_trajs     : integer
                       number of trajectories
        save_trajs   : boolean (optional)
                       If True, entire trajectories are saved rather than
                       saving only the last steps needed for plotting.
                       False by default.
        with_dots    : boolean (optional)
                       If True, dots representative of random-walking entities
                       are displayed. Has precedence over `save_trajs`. 
                       False by default. 
        tpause       : float (optional)
                       Pausing time between 2 steps. .01 secondes by default.   
        """
        skoki = np.array([[0, 1], [1, 0], [-1, 0], [0, -1]])
        trajs = np.zeros((nb_trajs, 1, 2))
        for i in range(nb_steps):
            _steps = []
            for j in range(nb_trajs):
                traj = trajs[j,:,:]
                losy = np.random.randint(4, size = 1)
                temp = skoki[losy, :]
                traj = np.concatenate((traj, temp), axis = 0)
                traj[-1,:]   += traj[-2,:]
                _steps.append(traj)
            if save_trajs or with_dots:
                trajs = np.array(_steps)
                if with_dots:
                    plt.cla()
                    plt.plot(trajs[:,i, 0].T, trajs[:,i, 1].T, 'ro') ## There are leeway in avoiding these costly transpositions
                    plt.plot(trajs[:,:i+1, 0].T, trajs[:,:i+1, 1].T)
                else:
                    plt.plot(trajs[:,-1+i:i+1, 0].T, trajs[:,-1+i:i+1, 1].T)
            else:
                trajs = np.array(_steps)[:,-2:,:]
                plt.plot(trajs[:,:, 0].T, trajs[:,:, 1].T)
    
            plt.pause(tpause)
    
    
    real_time_random_walk_2D_NT(50, 6, with_dots=True)
    

    real_time_random_walk_2D_NT(50, 6)
    

    【讨论】:

    • 它有效,但我真的需要生成的不仅是一个随机游走,还有几个。您的代码会为每个步骤生成不同的图。我想要一个情节。
    • @Hendrra。但是你必须为每一步生成不同的图,仅仅因为红点移动。没有这个红点,你是对的,你可以简单地累积步骤而不重新绘制所有内容。
    • 可惜了。生成随机游走后加红点怎么样?
    • 这正是我想要的!谢谢你。是否可以使用相同的方法来创建 3D 随机游走?
    • 对不起!我忘了勾选你的答案是正确的。我会考虑一段时间,我确定我需要询问一些细节:)
    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多