I want to update the legends in using the ArtistAnimation from Matplotlib.
I try to adapt this solution : Matplotlib animation update title using ArtistAnimation to the legend but it didn't worked. I tried a bunch of others things too but nothing seems to work.
In this case, I got no error, the result is just not what's expected. The legend appears 1 time over 3, only for the green. And the result I want is that the legends change each time with the good color and label.
Here is a minimal example of what I've done :
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import animation
import numpy as np
a = np.random.rand(3,3)
fig, ax=plt.subplots()
container = []
colors = ["red","blue","green"]
labels = [0,1,2]
for i in range(a.shape[1]):
line, = ax.plot(a[:,i], color=colors[i])
title = ax.text(0.5,1.05,"Title {}".format(i),
size=plt.rcParams["axes.titlesize"],
ha="center", transform=ax.transAxes, )
legend = ax.legend(handles=[mpl.patches.Patch(color=colors[i], label=labels[i])])
container.append([line, title, legend])
ani = animation.ArtistAnimation(fig, container, interval=750, blit=False)
#ani.save('videos/test.gif')
plt.show()