Python 使用动画在绘图上着色

Python 使用动画在绘图上着色,python,animation,matplotlib,Python,Animation,Matplotlib,我尝试在地图上着色,以显示点在使用动画移动时的“探索区域”。这是我目前掌握的代码: 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.f

我尝试在地图上着色,以显示点在使用动画移动时的“探索区域”。这是我目前掌握的代码:

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动画新手。

如果您希望添加的圆圈在屏幕上保持不变,您可能不应该使用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()

如果你想使用BLIT,考虑使用一条线来标记圆所在的区域。

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)