2017-04-17 94 views
0

我试图在地图上阴影以显示使用FuncAnimation移动的点的“探测区域”。这是我到目前为止的代码:使用funcanimation绘制阴影

import matplotlib.pyplot as plt 
import matplotlib.patches as patches 
import matplotlib.animation as animation 
import numpy as np 
import random 
import scipy.stats as stats 

map_x = 100 
map_y = 100 
fig = plt.figure(0) 
plt.figure(0) 
ax1 = plt.subplot2grid((2,3), (0,0), colspan=2, rowspan=2) 
ax2 = plt.subplot2grid((2,3), (0,2), colspan=1) 
ax1.set_xlim([0, map_x]) 
ax1.set_ylim([0, map_y]) 
ax2.set_xlim([0, map_x]) 
ax2.set_ylim([0, map_y]) 
agent = plt.Circle((50, 1), 2, fc='r') 
agent2 = plt.Circle((50, 1), 2, fc='r') 
agents = [agent, agent2] 
ax1.add_patch(agent) 
ax2.add_patch(agent2) 

def animate(i): 
    x, y = agent.center 
    x = x+.1 
    y = y+.1 
    agent.center = (x, y) 
    agent2.center = (x, y) 
    return agent, 

def fillMap(x, y): 
    circle=plt.Circle((x,y), 4, fc='b') 
    ax2.add_patch(circle) 

def animate2(i): 
    x, y = agent2.center 
    x = x+.1 
    y = y+.1 
    agent2.center = (x, y) 
    fillMap(x, y) 
    return agent2, 

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True) 
anim2 = animation.FuncAnimation(fig, animate2, frames=200, interval=20, blit=True) 

plt.show() 

但是,它只进入fillMap一次,只有吸引,而不是随处可见,其中的红点在那张小插曲充满圆一次蓝色。任何帮助,这是非常感谢,我是新来的动画python。

回答

0

如果你想要添加的圆圈在屏幕上保持不变,你可能不应该使用blitting。不使用blitting会使动画变慢,但每20步左右画一个新的蓝色圆就足够了。

import matplotlib.pyplot as plt 
import matplotlib.patches as patches 
import matplotlib.animation as animation 
import numpy as np 
import scipy.stats as stats 

map_x = 100 
map_y = 100 
fig = plt.figure(0) 
plt.figure(0) 
ax1 = plt.subplot2grid((2,3), (0,0), colspan=2, rowspan=2) 
ax2 = plt.subplot2grid((2,3), (0,2), colspan=1) 
ax1.set_xlim([0, map_x]) 
ax1.set_ylim([0, map_y]) 
ax2.set_xlim([0, map_x]) 
ax2.set_ylim([0, map_y]) 
agent = plt.Circle((50, 1), 2, fc='r') 
agent2 = plt.Circle((50, 1), 2, fc='r') 
agents = [agent, agent2] 
ax1.add_patch(agent) 
ax2.add_patch(agent2) 


def fillMap(x, y): 
    circle=plt.Circle((x,y), 4, fc='b') 
    ax2.add_patch(circle) 

def animate2(i): 
    x, y = agent.center 
    x = x+.1 
    y = y+.1 
    agent.center = (x, y) 
    x, y = agent2.center 
    x = x+.1 
    y = y+.1 
    agent2.center = (x, y) 
    if i%20==0: 
     circle = fillMap(x, y) 


anim2 = animation.FuncAnimation(fig, animate2, frames=200, interval=20, blit=False) 

plt.show() 

如果您想使用blitting,请考虑使用一条线来标记该圆圈所在的区域。

line, =ax2.plot([],[], lw=3, color="b") 
xl = []; yl=[] 
def fillMap(x, y): 
    xl.append(x); yl.append(y) 
    line.set_data(xl,yl) 
    return line 

def animate2(i): 
    x, y = agent.center 
    x = x+.1 
    y = y+.1 
    agent.center = (x, y) 
    x, y = agent2.center 
    x = x+.1 
    y = y+.1 
    agent2.center = (x, y) 
    if i%20==0: 
     fillMap(x, y) 
    return agent, agent2, line 

anim2 = animation.FuncAnimation(fig, animate2, frames=200, interval=20, blit=True)