用funcanimation对阴影进行着色

时间:2017-04-17 21:31:29

标签: python animation matplotlib

我试图遮盖地图以显示"探索地区"使用FuncAnimation移动时的点数。这是我到目前为止的代码:

<script src='//cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js'></script>

然而,它只进入fillMap一次,并且只绘制一次填充蓝色的圆圈,而不是在较小的子图中红点所在的任何地方。非常感谢任何帮助,我不熟悉python中的动画。

1 个答案:

答案 0 :(得分: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)