I'm trying to get a different color for each point in this animation. I would like to pass the array c_state as the hue-value of a hsv-tuple for each point. So far everything I tried has failed. I've tried using this in the animate function:
particles.set_color(pbox.color[:,0],1.0,1.0)
But I'm getting warning that only length-1 arrays can be converted to scalars. I've also tried making length-3 arrays using np.random and trying to convert those to rgb-tuples but that didn't work either. I have trouble finding the right data-structure to pass to the color variable of ax.plot. The colors only have to be set once and don't need to change during the animation.
import matplotlib.pyplot as plt
import matplotlib.animation as anim
import numpy as np
import colorsys
from random import random
n = 250
class ParticleBox:
def __init__(self,i_state,c_state):
self.i_state = np.asarray(i_state, dtype=float)
self.c_state = np.asarray(c_state, dtype=float)
self.state = self.i_state.copy()
self.color = self.c_state.copy()
i_state = -5 + 10 * np.random.random((n, 2))
c_state = np.random.random((n, 1))
pbox = ParticleBox(i_state, c_state)
fig = plt.figure()
ax = fig.add_subplot(111, xlim=(-10,10), ylim=(-10,10))
particles, = ax.plot([], [], 'o', ms=5)
def init():
global pbox
particles.set_data([],[])
return particles,
def animate(i):
global pbox, ax, fig
particles.set_data(pbox.state[:,0],pbox.state[:,1])
return particles,
ani = anim.FuncAnimation(fig, animate, frames = 500,
interval = 10, blit=True,
init_func=init)
plt.show()
