def main(): numframes = 30*simulation_time numpoints = n color_data = np.random.random((numframes, numpoints)) #x, y = [person.posx[0] for person in persons],[person.posy[0] for person in persons] #color_data = [person.cat for person in persons] fig = plt.figure() axis = plt.axes(xlim = (0,sz), ylim = (0,sz)) scat = plt.scatter([person.posx for person in persons],[person.posy for person in persons], c = np.asarray([person.cat for person in persons])) #scat.set_array([person.cat for person in persons]) anim = animation.FuncAnimation(fig, update_plot, frames=numframes,fargs=(color_data,scat)) anim.save('p1.mp4', writer = 'ffmpeg', fps = 30) plt.show() def update_plot(i,color_data, scat): for person in persons: person.update_pos(1) for person in persons: if person.quar == True or person.cat != 1: continue for neighbour in persons: if neighbour.cat!=2 or person.ID == neighbour.ID or np.random.rand()>trans_prob: continue if(np.sqrt((person.posx-neighbour.posx)**2+(person.posy-neighbour.posy)**2)<inf_rad): person.cat=2 person.tm = 0 for person in persons: if person.cat==2 and person.tm>=recov_tm: person.cat = 3 person.quar = False elif person.cat==2 and person.quar==False and np.random.rand()<quar_prob: person.quar = True data = np.zeros((n,3)) data[:,0] = [person.posx for person in persons] data[:,1] = [person.posy for person in persons] data[:,2] = [person.cat for person in persons] scat.set_offsets(data[:, :2]) scat.set_array(data[:, 2]) return scat, main() I am trying to animate a scatter plot. Person is my object which has several attributes. One of the attributes is person.cat which takes discrete values [1,2,3]. I want my animation to be colored with [r,g,b] respectively according to person.cat. These values keep updating so I need to keep my colors updated. My code gives some error in color mapping as mentioned above. Is there any solution for it?
For reference below is the constructor for person object of ind class:
class ind: def __init__(self, ID): self.ID = ID #self.init_posx = np.random.rand(1)*sz #self.init_posy = np.random.rand(1)*sz self.posx = np.random.rand(1)*sz self.posy = np.random.rand(1)*sz self.tm = 0 self.spd = np.random.rand(1)*max_spd self.theta = np.random.rand(1)*2*np.pi self.cat = 1 #this can be either of 1,2,3. self.rec_tm = recov_tm self.quar = False