I have a plot consisting of a large number of lines. At each step, the colors of the lines should be updated in the animation, but doing a for loop on the lines seems very expensive. Is there a better way to do this?
Here is my code:
import numpy as np lines=[] from matplotlib import pyplot as plt import matplotlib.animation as animation #initial plot fig=plt.figure() ax=plt.subplot(1,1,1) for i in range(10): lines.append([]) for j in range(10): lines[i].append(ax.plot([i,j],color='0.8')) lines=np.asarray(lines) ##Updating the colors 10 times im=[] for steps in range(10): colors=np.random.random(size=(10,10)) for i in range(10): for j in range(10): lines[i,j][0].set_color(str(colors[i,j])) plt.draw() # im.append(ax) plt.pause(.1) #ani = animation.ArtistAnimation(fig, im, interval=1000, blit=True,repeat_delay=1000) plt.show()
Plus, I could not get him to work with an animated artist! I used a draw. What is wrong with animation lines
Now increasing these 10 s to 100 makes the program terribly slow:
import numpy as np lines=[] from matplotlib import pyplot as plt import matplotlib.animation as animation #initial plot fig=plt.figure() ax=plt.subplot(1,1,1) for i in range(100): lines.append([]) for j in range(100): lines[i].append(ax.plot([i,j],color='0.8')) lines=np.asarray(lines) ##Updating the colors 10 times im=[] for steps in range(10): colors=np.random.random(size=(100,100)) for i in range(100): for j in range(100): lines[i,j][0].set_color(str(colors[i,j])) plt.draw() # im.append(ax) plt.pause(.1) #ani = animation.ArtistAnimation(fig, im, interval=1000, blit=True,repeat_delay=1000) plt.show()
As I said, I want to run it side by side with animation. Therefore, I prefer to make animation. I think this would solve the problem with the delay, at least after the start of the animation, but right now, as I defined it, it does not work.