2017-02-09 77 views
0

我目前正在研究交通拥堵分析,并且想知道是否有办法让这种堵塞的情节生成动画。Matplotlib动画空间与时间的关系图

这个事情的情节从图的下端开始增长,每个'行'是一个时间实例。水平轴仅仅是指示每个点处每个车辆的位置以及具有特定数值的速度的道路。因此,将不同的颜色应用于不同的速度,您会得到一个图表,显示在给定的道路上,随着时间的推移,果酱会如何演变。

我的问题是,我该如何使用matplotlib来及时生成每个道路实例的动画来获得这样的情节?

情节是这样的:

我通过模拟与时间具有一定速度的车辆的道路,所以我希望动画显示交通拥堵如何演变的阴谋......

编辑:

我添加一些代码来明确一下我已经在做

import numpy as np 

from matplotlib import pyplot as plt 
from matplotlib import animation, rc 

plt.rcParams['animation.ffmpeg_path'] = u'/usr/bin/ffmpeg' 

# model params 
vmax = 5 
lenroad = 50 
prob = 0.4 

# sim params 
numiters = 10 

# traffic model 
def nasch(): 
    gaps = np.full(road.shape, -1) 
    road_r4 = np.full(road.shape, -1) 
    for n,x in enumerate(road): 
     if x > -1: 
      d = 1 
      while road[(n+d) % len(road)] < 0: 
       d += 1 
      d -= 1 
      gaps[n] = d 

    road_r1 = np.where(road!=-1, np.minimum(road+1, vmax), -1) 
    road_r2 = np.where(road_r1!=-1, np.minimum(road_r1, gaps), -1) 
    road_r3 = np.where(road_r2!=-1, np.where(np.random.rand() < prob, np.maximum(road-1, 0), road), -1) 
    for n,x in enumerate(road_r3): 
     if x > -1: 
      road_r4[(n+x) % len(road_r3)] = x 

    return road_r4 

def plot_nasch(*args): 
    road = nasch() 
    plot.set_array([road]) 
    return plot, 

# init road 
road = np.random.randint(-10, vmax+1, [lenroad]) 
road = np.where(road>-1, road, -1) 

# simulate 
fig = plt.figure() 
plot = plt.imshow([road], cmap='Pastel2', interpolation='nearest') 
for i in range(numiters): 
    ani = animation.FuncAnimation(fig, plot_nasch, frames=100, interval=500, blit=True) 
    plt.show() 

我也得到如下图所示,只需一条公路,而不是画在前面一个底部每道:

enter image description here

+1

是的,这是绝对可能的matplotlib。你可以将你的模拟结果放在一个numpy数组中,用'imshow'显示并使用'FuncAnimation'对其进行动画处理。每个涉及的步骤都有足够的例子。 – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest其实这就是我想要做的,问题在于,每次我使用FuncAnimation时,绘制的道路都会在同一点覆盖之前的道路,而不是在每一步都会向下“...” –

+0

如果您有你需要明确地询问这个问题,将你的代码减少到[mcve],清楚地说明输出是什么,以及它与你期望的偏离多远。 – ImportanceOfBeingErnest

回答

0

这可能是你想要的,虽然我不知道你为什么想要动画化时间,因为时间已经是情节中的轴之一。

这里的想法是将行中的时间步的模拟结果存储在一个数组中并重新绘制这个数组。从而以前的模拟结果不会丢失。

import numpy as np 

from matplotlib import pyplot as plt 
from matplotlib import animation, rc 

# model params 
vmax = 5 
lenroad = 50 
prob = 0.4 

# sim params 
numiters = 25 

# traffic model 
def nasch(): 
    global road 
    gaps = np.full(road.shape, -1) 
    road_r4 = np.full(road.shape, -1) 
    for n,x in enumerate(road): 
     if x > -1: 
      d = 1 
      while road[(n+d) % len(road)] < 0: 
       d += 1 
      d -= 1 
      gaps[n] = d 

    road_r1 = np.where(road!=-1, np.minimum(road+1, vmax), -1) 
    road_r2 = np.where(road_r1!=-1, np.minimum(road_r1, gaps), -1) 
    road_r3 = np.where(road_r2!=-1, np.where(np.random.rand() < prob, np.maximum(road-1, 0), road), -1) 
    for n,x in enumerate(road_r3): 
     if x > -1: 
      road_r4[(n+x) % len(road_r3)] = x 

    return road_r4 


def plot_nasch(i): 
    print i 
    global road 
    road = nasch() 
    #store result in array 
    road_over_time[i+1,:] = road 
    # plot complete array 
    plot.set_array(road_over_time) 


# init road 
road = np.random.randint(-10, vmax+1, [lenroad]) 
road = np.where(road>-1, road, -1) 
# initiate array 
road_over_time = np.zeros((numiters+1, lenroad))*np.nan 
road_over_time[0,:] = road 



fig = plt.figure() 
plot = plt.imshow(road_over_time, cmap='Pastel2', interpolation='nearest', vmin=-1.5, vmax=6.5) 
plt.colorbar() 

ani = animation.FuncAnimation(fig, plot_nasch, frames=numiters, init_func=lambda : 1, interval=400, blit=False, repeat=False) 
plt.show()