So i copied this code from Animation based on only updating colours in a plot and made some slight adjusments. I have a plot and would like to change the color of a single line each frame. Code looks like this:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
import matplotlib.animation as animation
lines=[]
points=[(1,2),(5,4),(1,3),(2,1),(3,5)]
for i in range(len(points)-1):
lines.append([points[i],points[i+1]])
fig, ax = plt.subplots()
colors = np.array([0.1,0.1,0.1,0.9])
col = LineCollection(lines, array=colors, cmap=plt.cm.bwr)
ax.add_collection(col)
ax.autoscale()
print(colors)
def update(i):
colors[i%4]=np.random.random()
col.set_array(colors)
return col,
ani = animation.FuncAnimation(fig, update, interval=2000, blit=True,
init_func=lambda: [col])
plt.show()
While it does work if i leave it this way, i initially want all the lines to have the same color. When i change the initial colors to
colors = np.array([0.9,0.9,0.9,0.9])
instead of
colors = np.array([0.1,0.1,0.1,0.9])
the plot just stops from updating and stays one color the entire time. If i change just one number from the colors array, it works. Why would that be and what should i change for it to work?