So the idea is that I want to reposition the inner point of a circle randomly after each animation (so after the 128th frame in this case). Since Repeat=True, this should yield an ongoing animation where new circles keep appearing randomly after the other ones disappeared (due to my choice of the gist_gray colormap). So I thought doing something like
if i == 0:
x = randint(-2, 2)
y = randint(-2, 2)
at the beginning of my update function should work, but the animation just keeps crashing. It works when I initalise the variables outside of the update function though. There might be an error concerning the start of the animation, because when I put print(i) at the beginning of the update function, it gives
0
0
0
1
2
so for one reason or another, FuncAnimation seems to work with three zeros at the beginning. Does anybody have an idea how to get around that? The whole code:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
from random import randint
colors = plt.cm.gist_gray(np.linspace(0, 1, 256))
fig, ax = plt.subplots()
fig.set_size_inches(5, 5)
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.axis('off')
def update(i, fig, ax):
if i == 0:
x = randint(-2, 2)
y = randint(-2, 2)
circle_1 = plt.Circle((x, y), i / 100, color=colors[2*i])
circle_1.set_facecolor('white')
circle_2 = plt.Circle((x, y), i / 200, color=colors[2*i])
circle_2.set_facecolor('white')
circle_3 = plt.Circle((x, y), i / 400, color=colors[2*i])
circle_3.set_facecolor('white')
ax.add_artist(circle_1)
ax.add_artist(circle_2)
ax.add_artist(circle_3)
return [circle_1, circle_2, circle_3]
anim = FuncAnimation(fig, update, frames=128, interval=1, blit=True, repeat=True, fargs=(fig, ax))
plt.show()
Aucun commentaire:
Enregistrer un commentaire