我正在用python做寻路算法实现,我有一个网格并获得了路径。因此,我想使用FuncAnimation对从起点到目标的点序列进行动画处理。我尝试编写此代码,但它没有给我一行。这就是我所做的,在此示例代码中,我给出了网格和路径(网格中的位置x,y),但是输出只是没有路径的网格,我不知道该怎么做才能解决问题,请帮忙!
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def mazeanimation(grid, route):
fig, ax = plt.subplots(figsize=(10,10))
ax.imshow(grid, cmap=plt.cm.terrain)
line, = ax.plot([],[], color="black")
def init():
line.set_data([],[])
return line,
def animate(i):
x = route[i][0]
y = route[i][1]
line.set_data(x, y)
return line,
FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
grid = np.array([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
route = [(13, 1), (12, 2), (11, 3), (10, 4), (9, 5), (8, 5),
(7, 5), (6, 5), (5, 5), (4, 5), (3, 5), (2, 6), (2, 7),
(2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (2, 13),
(2, 14), (3, 15)]
mazeanimation(grid, route)
答案 0 :(得分:0)
您可以通过使用matplotlib
的交互模式来实现此目的:
import numpy as np
import matplotlib.pyplot as plt
#inputs###
grid = np.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
route = [(13, 1), (12, 2), (11, 3), (10, 4), (9, 5), (8, 5),
(7, 5), (6, 5), (5, 5), (4, 5), (3, 5), (2, 6), (2, 7),
(2, 8), (2, 9), (2, 10), (2, 11), (2, 12), (2, 13),
(2, 14), (3, 15)]
#setup the plot
fig, ax = plt.subplots(figsize=(10,10))
ax.imshow(grid, cmap=plt.cm.terrain)
line, = ax.plot([],[], color="black")
##### set interactive mode####
plt.ion()
plt.show()
for i,j in route:
#set up the canvas
plt.gcf().canvas.draw()
line, = ax.plot(i,j,'.',c='black')
plt.pause(0.1)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], '.',c='black')
def init():
ax.imshow(grid, cmap=plt.cm.terrain)
return ln,
def update(frame):
xdata.append(frame[0])
ydata.append(frame[1])
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=route,init_func=init, blit=True)
plt.show()