2017-04-17 84 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)