以下是动画和保存动画图的代码:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
fig = plt.figure("Map")
ax = plt.axes()
iteration = 0
x_boundary = 282
y_boundary = 282
color = ['r', 'b', 'g', 'y', 'c', 'b']
# color = ['black', 'dimgray', 'dimgrey', 'gray', 'grey', 'darkgray','darkgrey']
def animate(i):
global iteration
# read input text file from matlab and set target list
input_file = open("liveCoordinate.txt","r")
input_matrix = []
while True:
input_line = input_file.readline()
if len(input_line) == 0:
break
input_line = input_line.rstrip("\n")
input_matrix.append(input_line.split(","))
for i in range(len(input_matrix)):
for j in range(len(input_matrix[0])):
input_matrix[i][j] = float(input_matrix[i][j])
# plot data
if (len(input_matrix) > iteration):
# Clear out old stuffs
ax.clear() # graph
circle = [] # patches
# Draw the boundary
ax.add_patch(plt.Rectangle((-x_boundary/2, -y_boundary/2), x_boundary, y_boundary, fill = 0))
# Draw the xy axis
ax.add_patch(plt.Rectangle((-x_boundary/2, 0), x_boundary, 0, fill = 0))
ax.add_patch(plt.Rectangle((0, -y_boundary/2), 0, y_boundary, fill = 0))
graph_data = input_matrix[iteration]
for j in range(0,len(input_matrix[0]),2):
# Draw the agent
# ax.add_patch(plt.Circle((input_matrix[iteration][j], input_matrix[iteration][j+1]), radius=15, fc='r', fill = 0, color = [0,0,0]))
ax.add_patch(plt.Circle((input_matrix[iteration][j], input_matrix[iteration][j+1]), radius=15, fc='r', fill = 0, color = color[int(j/2)]))
ax.add_patch(plt.Circle((input_matrix[iteration][j], input_matrix[iteration][j+1]), radius=2, fc='b', fill = 1, color = color[int(j/2)]))
plt.axis('scaled')
iteration = iteration + 1
ani = animation.FuncAnimation(fig, animate, interval=100)
ani.save("simulation.mp4")
plt.show()
这是文件“ liveCoordinate.txt”:
-94,-94,94,-94,94,94,-94,94,0,0
-94,-94,94,-94,94,94,-94,94,0,0
-94,-94,94,-94,92,94,-94,94,0,0
-92,-94,93,-92,91,94,-93,92,0,0
-91,-94,93,-88,89,94,-93,89,1,2
-89,-94,93,-88,86,94,-92,87,2,3
-86,-95,92,-85,84,95,-92,85,4,5
-84,-95,92,-82,81,95,-91,83,5,6
-82,-96,91,-79,79,95,-91,80,7,8
-79,-96,90,-73,77,95,-90,78,8,9
-77,-97,90,-70,74,95,-90,75,10,11
-74,-97,90,-70,71,95,-89,72,11,13
-72,-98,90,-67,68,96,-88,70,13,15
-70,-98,89,-64,66,96,-88,67,14,17
-67,-99,89,-61,63,96,-87,64,16,18
-65,-99,89,-58,63,96,-86,61,18,20
-62,-99,88,-55,60,96,-86,59,19,22
-60,-100,87,-48,54,97,-85,56,21,23
-57,-100,87,-45,51,97,-85,51,21,23
-54,-100,87,-42,48,97,-85,51,22,24
-52,-100,87,-39,45,98,-84,48,22,24
-49,-100,87,-35,42,98,-84,43,23,24
-47,-100,87,-32,39,98,-84,43,23,24
-44,-100,87,-29,36,98,-84,41,24,25
-41,-100,86,-25,33,98,-83,38,25,25
-39,-101,86,-22,30,99,-83,33,26,25
-36,-101,86,-19,27,99,-83,33,26,25
-33,-100,85,-16,24,99,-83,30,26,25
-28,-100,85,-13,21,99,-82,28,26,25
-28,-100,85,-10,18,99,-82,25,26,25
-25,-100,85,-6,15,99,-82,22,26,25
-23,-100,85,-3,12,100,-81,17,26,25
-20,-100,85,0,9,100,-81,17,26,25
-17,-100,85,3,5,100,-81,14,26,25
-14,-100,85,6,2,100,-81,9,26,25
-11,-100,85,9,0,100,-81,9,26,25
-9,-100,85,12,-3,100,-81,6,26,25
-6,-100,86,16,-6,100,-80,4,26,25
-3,-100,86,19,-9,100,-80,1,25,25
0,-99,86,22,-12,100,-80,-1,25,25
2,-99,86,25,-16,100,-80,-3,25,25
4,-99,86,29,-19,100,-80,-6,25,25
7,-99,86,32,-22,100,-80,-9,25,25
10,-99,86,35,-25,100,-80,-12,25,25
13,-98,87,42,-34,100,-81,-20,25,25
19,-98,87,45,-34,100,-81,-20,25,25
22,-98,87,48,-37,100,-81,-23,25,25
24,-98,87,51,-41,100,-81,-26,25,25
27,-97,88,55,-44,100,-82,-28,25,25
30,-97,88,58,-47,100,-82,-31,25,25
33,-97,88,61,-50,100,-83,-34,25,25
36,-97,88,64,-53,100,-83,-37,25,25
39,-96,88,67,-56,100,-84,-40,25,25
42,-96,89,71,-59,100,-84,-43,25,25
45,-96,89,74,-62,99,-84,-46,25,25
48,-96,89,77,-65,99,-85,-48,25,25
51,-96,89,80,-68,99,-85,-51,25,25
53,-95,90,83,-71,98,-86,-54,25,25
56,-95,90,86,-74,98,-86,-57,25,25
62,-95,91,89,-77,98,-87,-60,25,25
62,-95,92,92,-80,97,-88,-63,25,25
65,-95,92,94,-83,97,-88,-66,25,25
71,-94,93,95,-86,96,-90,-72,25,25
71,-94,94,95,-89,96,-90,-72,25,25
74,-94,94,95,-91,95,-91,-75,25,25
77,-94,94,94,-92,95,-92,-81,25,25
83,-94,94,93,-93,94,-93,-84,25,25
83,-94,94,93,-94,93,-93,-84,25,25
86,-94,94,93,-95,93,-94,-89,25,25
92,-93,94,93,-95,93,-94,-91,25,25
94,-93,94,93,-95,93,-94,-93,25,25
95,-93,94,93,-95,93,-94,-94,25,25
96,-92,93,94,-95,92,-93,-95,20,26
97,-90,91,94,-96,90,-92,-95,20,26
97,-87,89,94,-96,88,-90,-96,18,26
98,-84,87,95,-97,86,-89,-97,15,26
99,-79,85,95,-97,83,-88,-99,13,26
100,-76,83,95,-97,80,-87,-100,8,26
100,-73,81,95,-97,73,-85,-101,6,26
100,-70,78,96,-97,70,-83,-102,3,26
101,-66,76,97,-97,70,-82,-102,1,26
101,-63,74,97,-97,67,-82,-102,1,26
101,-60,71,98,-97,64,-79,-102,-1,27
101,-56,68,98,-97,61,-79,-102,-4,27
101,-53,65,99,-96,58,-76,-103,-10,28
101,-50,62,99,-96,54,-75,-103,-12,28
101,-46,60,100,-96,51,-75,-103,-12,28
101,-43,57,100,-96,48,-73,-103,-15,28
101,-43,54,101,-96,44,-70,-102,-20,28
101,-39,51,102,-96,41,-70,-102,-20,28
101,-33,48,102,-96,35,-66,-101,-22,27
101,-33,45,103,-96,35,-66,-101,-22,27
101,-29,42,103,-96,32,-64,-101,-23,27
101,-26,39,103,-96,28,-62,-100,-24,25
100,-22,36,104,-96,25,-57,-99,-25,25
100,-16,33,104,-96,21,-57,-99,-25,25
100,-16,30,105,-95,18,-55,-99,-25,25
100,-12,27,105,-95,15,-53,-99,-25,24
100,-5,23,105,-95,11,-48,-98,-25,24
100,-5,20,105,-95,8,-48,-98,-25,24
100,-2,17,105,-95,5,-46,-98,-25,24
100,0,14,106,-95,1,-41,-97,-25,24
100,7,11,106,-95,-1,-38,-96,-25,24
100,11,8,106,-95,-4,-36,-95,-25,24
100,11,5,106,-95,-8,-36,-95,-25,24
99,14,2,106,-95,-14,-30,-94,-25,24
99,21,-4,106,-95,-18,-27,-93,-25,24
99,24,-7,106,-95,-21,-25,-93,-25,24
99,27,-10,106,-94,-24,-22,-92,-25,24
98,31,-13,107,-94,-27,-19,-92,-25,24
98,34,-16,106,-94,-31,-17,-91,-25,24
98,37,-19,106,-94,-34,-14,-90,-25,24
98,41,-23,106,-94,-37,-11,-90,-25,24
98,44,-26,106,-94,-40,-8,-89,-25,24
97,47,-29,106,-94,-44,-6,-89,-25,24
97,51,-32,106,-94,-47,-3,-88,-25,24
97,54,-35,106,-94,-50,0,-88,-25,24
97,61,-38,106,-94,-54,2,-87,-25,24
97,61,-41,106,-94,-57,4,-87,-25,24
96,64,-44,105,-94,-60,7,-87,-25,24
96,68,-47,105,-94,-64,12,-86,-25,24
96,71,-50,105,-94,-67,12,-86,-25,24
96,75,-53,105,-94,-70,15,-86,-25,24
96,78,-56,104,-94,-74,17,-86,-25,24
95,81,-59,104,-94,-77,20,-86,-25,24
95,84,-62,104,-94,-80,23,-85,-25,24
95,88,-65,103,-94,-84,28,-85,-25,24
94,93,-68,103,-94,-87,31,-85,-25,24
94,94,-71,102,-94,-90,31,-85,-25,24
94,94,-73,102,-94,-93,36,-85,-25,24
94,95,-76,101,-94,-95,38,-85,-25,24
94,95,-79,100,-94,-95,41,-85,-25,24
94,95,-81,99,-94,-95,44,-86,-25,24
94,95,-84,99,-94,-95,46,-86,-25,24
94,95,-87,98,-94,-95,49,-86,-25,24
94,95,-89,97,-94,-95,51,-86,-25,24
94,95,-91,96,-94,-95,54,-86,-25,24
94,94,-92,95,-94,-94,56,-85,-25,24
94,94,-92,95,-94,-94,59,-85,-25,24
94,94,-93,94,-94,-94,61,-85,-25,24
94,94,-94,94,-94,-94,64,-85,-25,24
94,94,-95,93,-94,-94,66,-85,-25,24
94,94,-95,93,-94,-94,68,-86,-25,24
94,94,-95,93,-94,-94,71,-86,-25,24
94,94,-94,93,-94,-94,74,-87,-25,24
94,94,-94,93,-94,-94,76,-87,-25,24
94,94,-94,93,-94,-94,79,-88,-25,24
94,94,-94,93,-94,-94,82,-88,-25,24
94,94,-94,93,-94,-94,85,-89,-25,24
94,94,-94,93,-94,-94,87,-90,-25,24
94,94,-94,93,-94,-94,90,-90,-25,24
94,94,-94,93,-94,-94,91,-91,-25,24
94,94,-94,93,-94,-94,92,-92,-25,24
94,94,-94,93,-94,-94,93,-93,-25,24
93,94,-95,91,-93,-94,94,-90,-26,24
91,95,-95,88,-93,-94,95,-88,-26,22
90,95,-95,86,-92,-94,96,-60,-24,-6
37,104,-92,28,-55,-97,96,-23,-25,-25
34,105,-92,24,-52,-97,96,-23,-25,-25
31,105,-92,21,-50,-96,96,-17,-25,-25
31,105,-92,18,-47,-96,96,-17,-25,-25
27,106,-92,15,-45,-96,96,-10,-25,-25
24,106,-92,12,-43,-95,96,-10,-25,-25
18,107,-92,9,-40,-95,96,-3,-25,-25
15,107,-92,5,-37,-94,96,0,-25,-25
15,107,-92,2,-35,-94,96,0,-25,-25
11,108,-91,0,-32,-93,96,6,-25,-25
8,108,-91,-3,-30,-93,96,9,-25,-25
5,108,-91,-6,-27,-93,96,9,-25,-25
-1,108,-91,-10,-25,-92,96,16,-25,-25
-4,108,-91,-13,-22,-92,96,19,-25,-25
-7,108,-91,-16,-19,-92,96,19,-25,-25
-7,108,-91,-19,-17,-91,96,26,-25,-25
-10,108,-91,-22,-14,-91,96,26,-25,-25
-13,108,-91,-25,-11,-91,96,29,-25,-25
-17,108,-91,-29,-8,-91,96,33,-25,-25
-20,108,-91,-32,-6,-90,96,39,-25,-25
-26,107,-91,-35,-3,-90,96,43,-25,-25
-29,107,-91,-38,0,-90,96,43,-25,-25
-29,107,-91,-41,2,-90,96,46,-25,-25
-32,107,-92,-45,7,-90,95,53,-25,-25
-38,107,-92,-48,7,-90,95,56,-25,-25
-38,107,-92,-51,10,-90,95,56,-25,-25
-41,107,-92,-54,13,-90,95,63,-25,-25
-44,106,-92,-58,15,-90,95,66,-25,-25
-47,106,-92,-61,18,-90,95,66,-25,-25
-50,106,-92,-64,21,-90,95,70,-25,-25
-57,106,-92,-68,24,-90,95,76,-25,-25
-60,105,-92,-71,27,-90,95,80,-25,-25
-63,105,-92,-74,30,-90,95,80,-25,-25
-66,104,-93,-78,32,-89,94,86,-25,-25
-68,104,-93,-81,35,-89,94,90,-25,-25
-71,103,-93,-84,38,-89,94,90,-25,-25
-74,103,-93,-87,40,-89,94,95,-25,-25
-76,102,-93,-90,43,-90,94,96,-25,-25
-79,101,-94,-92,46,-90,94,96,-25,-25
-81,100,-94,-94,49,-90,94,96,-25,-25
-84,99,-94,-95,52,-90,94,96,-25,-25
-87,98,-94,-95,55,-90,94,95,-25,-25
-87,98,-94,-95,57,-90,94,95,-25,-25
-89,97,-94,-95,60,-90,94,95,-25,-25
-91,96,-94,-95,63,-90,94,94,-25,-25
-92,96,-94,-95,66,-91,95,94,-25,-25
-93,94,-94,-95,69,-91,95,94,-25,-25
-93,94,-94,-95,71,-91,95,94,-25,-25
-94,94,-94,-95,74,-91,95,94,-25,-25
-95,93,-95,-95,77,-91,95,94,-25,-25
-95,93,-95,-94,80,-91,95,94,-25,-25
-95,93,-95,-94,83,-91,95,94,-25,-25
-95,93,-95,-93,86,-92,95,94,-25,-25
-95,93,-95,-93,89,-92,95,94,-25,-25
-95,93,-95,-93,92,-92,95,94,-25,-25
我从“ simulation.mp4”中得到的是一个不完整的动画图。该文件只有11秒长,而图形可以运行30秒以上。另外,我希望可以在plt.show()运行时保存文件,但是显然plt.show()仅在animate.save完成后才能运行,我想这是有道理的。
有什么办法可以解决这个琐碎的问题?谢谢。