Вы можете добиться этого, используя интерактивный режим 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()