使用matplotlib对人群运动进行建模 [英] modelling crowd movement with matplotlib

查看:98
本文介绍了使用matplotlib对人群运动进行建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用python建立基本的人群运动模型.我想展示一个动画.我已经制作了以下程序来使用matplotlib对其进行测试:

I would like to model basic crowd movement with python. I want to show an animation. I have made the following program to test it with matplotlib :

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

#size of the crowd
N = 100

def gen_data():
    """ init position and speed of each people """
    x = y = np.zeros(N)
    theta = np.random.random(N) * 360 / (2 * np.pi)
    v0 = 0.1
    vx, vy = v0 * np.cos(theta), v0 * np.sin(theta)
    return np.array([x, y, vx, vy]).T

def init():
    for line in lines:
        line.set_data([],[])
    return line,

def update_lines(i, lines, data):
    for d, line in zip(data, lines):
        d[0:2] += d[2:4]
        if abs(d[0]) > 5: d[2] *= -1
        if abs(d[1]) > 5: d[3] *= -1
        line.set_data(d[0] ,d[1])
    return lines

fig = plt.figure()
ax = plt.axes(xlim=(-5,5),ylim=(-5,5))
lines = [plt.plot([],[], 'ko')[0] for i in range(N)]

data = gen_data()

anim = animation.FuncAnimation(fig, update_lines, init_func=init, fargs=(lines, data), interval=10, blit=True)

plt.show()

即使对于N = 100,动画也很慢...我可以使用mathplotlib加快速度吗? matplotlib是用python制作稀薄动画的最佳图形工具吗?如果没有,那会是什么?

Even for N=100, the animation is slow... Is there something I can do to speed it up with mathplotlib ? Is matplotlib the best graphic tool to make thins kind of animation with python ? If no, what would it be ?

推荐答案

您可以执行以下3项操作来使动画更快:

Here are 3 things you can do to make the animation faster:

  • N呼叫替换为plt.plot,将一个呼叫替换为plt.scatter.
  • 用赋值一次替换data的整个切片的赋值替换update中的for-loop:

  • Replace the N calls to plt.plot with one call to plt.scatter.
  • Replace the for-loop in update with assignments which modify whole slices of data at once:

data[:, 0:2] += data[:, 2:4]
data[:, 2] = np.where(np.abs(data[:, 0]) > 5, -data[:, 2], data[:, 2])
data[:, 3] = np.where(np.abs(data[:, 1]) > 5, -data[:, 3], data[:, 3])

  • interval=10减小为interval=0.

  • Reduce interval=10 to interval=0.

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import animation
    
    # size of the crowd
    N = 100
    
    def gen_data():
        """ init position and speed of each people """
        x = y = np.zeros(N)
        theta = np.random.random(N) * 360 / (2 * np.pi)
        v0 = 0.1
        vx, vy = v0 * np.cos(theta), v0 * np.sin(theta)
        return np.column_stack([x, y, vx, vy])
    
    def init():
        pathcol.set_offsets([[], []])
        return pathcol,
    
    def update(i, pathcol, data):
        data[:, 0:2] += data[:, 2:4]
        data[:, 2] = np.where(np.abs(data[:, 0]) > 5, -data[:, 2], data[:, 2])
        data[:, 3] = np.where(np.abs(data[:, 1]) > 5, -data[:, 3], data[:, 3])
        pathcol.set_offsets(data[:, 0:2])
        return [pathcol]
    
    fig = plt.figure()
    ax = plt.axes(xlim=(-5, 5), ylim=(-5, 5))
    pathcol = plt.scatter([], [])
    data = gen_data()
    anim = animation.FuncAnimation(fig, update, init_func=init,
                                   fargs=(pathcol, data), interval=0, blit=True)
    plt.show()
    

    这篇关于使用matplotlib对人群运动进行建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆