如果要将多个元素一起设置动画,则不应运行多个
FuncAnimation
. 相反,运行一个动画,并在单个动画中为所有艺术家设置动画
animate()
功能。
我无法运行您的代码,但我试图接近我在本演示中可以理解的您的代码:
import math
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib import animation
from matplotlib.colors import rgb2hex
fig, ax = plt.subplots()
ax.axis("equal")
ax.set_xlim(-10, 10)
ax.set_ylim(-5, 5)
ax.axis("off")
state_nodes = [Circle(center, 0.2, color='k') for center in np.random.normal(loc=0, scale=1, size=(5,2))]
for state in state_nodes:
ax.add_artist(state)
def animate(i, states):
c = (i/255)
for state in states:
dx,dy = np.random.normal(loc=0, scale=0.1, size=(2,))
x,y = state.get_center()
state.set_center((x+dx,y+dy))
state.set_color(rgb2hex((c,c,c)))
return states
ani = animation.FuncAnimation(fig, animate, fargs = (state_nodes,), frames=201, interval=25, blit=True, repeat = False)
plt.show()