0

I have gotten a scatter plot working. Now I am trying to animate it. I have looked through multiple docs on how to do this. I get animation of the scatter plot, but none of the points are in the right position. I believe I have misunderstood something about how to use set_offsets, but I don't know what.

Here is the code in the class that calls matplotlib. It sets the agents in the right position in the initial plot:

def plot(self):                       
    Show where agents are in graphical form. -------------------------- 2---
    data = self.plot_data()             
    disp.display_scatter_plot("Agent Positions", data, anim=True,    
                              data_func=self.plot_data)    

def plot_data(self):                                                        
    data = {}                                                               
    for v in self.agents.varieties_iter():
        data[v] = {"x": [], "y": []}
        for agent in self.agents.variety_iter(v):
            x_y = self.get_pos_components(agent)
            data[v]["x"].append(x_y[0])
            data[v]["y"].append(x_y[1])
    return data

And here is my attempt to animate this plot:

def display_scatter_plot(title, varieties, anim=False,    
                         data_func=None):    
    """    
    Display a scatter plot.l              
    varieties is the different types of             
    entities to show in the plot, which    
    will get assigned different colors     
    """                                    

    def update_plot(i):    
        varieties = data_func()    
        for var, scat in zip(varieties, scats):    
            x = np.array(varieties[var]["x"])      
            y = np.array(varieties[var]["y"])      
            scat.set_offsets((x, y))             
        return scats                             

    fig, ax = plt.subplots()    

    scats = []                  
    i = 0         
    for var in varieties:
        color = colors[i % NUM_COLORS]
        x = np.array(varieties[var]["x"])
        y = np.array(varieties[var]["y"])
        scat = plt.scatter(x, y, c=color, label=var,
                           alpha=0.9, edgecolors='none')
        scats.append(scat)
        i += 1

    ax.legend()
    ax.set_title(title)
    plt.grid(True)

    if anim:
        animation.FuncAnimation(fig,
                                update_plot,
                                frames=1000,
                                interval=1000,
                                blit=False)

    plt.show(block=False)

Once the animation starts, the points do move around, but, as I mentioned, none of them to the right positions! As I said, I think I have gotten set_offsets wrong, but I don't know how I did so.

Ajean
  • 5,528
  • 14
  • 46
  • 69
  • Did you see http://stackoverflow.com/questions/9401658/matplotlib-animating-a-scatter-plot?rq=1? Because that seems to work, and also looks like it's using set_offsets differently. – cphlewis Feb 26 '15 at 01:56
  • That is one of the examples I drew on. I thought I was using set_ofsets() the way this example used it! (Clearly I am wrong, but what I don't understand is WHAT I've done wrong.) – Gene Callahan Feb 26 '15 at 06:53
  • You've set up really, really simple dummy data? in case you have the columns out of order or off-by-one or something? – cphlewis Feb 26 '15 at 07:56
  • What is the dtype & shape of varieties ? – Nicolas Rougier Feb 26 '15 at 08:03
  • Varieties is a dictionary (of different agent types) with a dictionary that holds a dictionary ("x" and "y") where x holds a list of x positions, and y holds a list of y positions. – Gene Callahan Feb 26 '15 at 15:18
  • cphlewis, this IS my little test case that I am running. The thing is, the initial positions are all fine, so I know I have the data set up correctly. I am (pretty) certain that I am just not passing the right thing to set_offsets(), but I am having trouble figuring out what the right thing to pass is: my case is different enough from the example you pointed to that I can't quite see what is wrong. – Gene Callahan Feb 26 '15 at 15:20
  • OK, more debugging: when I have two agents, the coordinates of one get read as the 2 x coordinates, and that of the other as the 2 y coordinates. So I have my data in the wrong order. I should have this fixed in a few minutes. – Gene Callahan Feb 26 '15 at 16:28
  • The problem was this: scatter() and scatter.set_offsets() expect the data in different forms: scatter wants an array of x's and an array of y's. But set_offsets() expects an array of x, y pairs. It is kind of weird that they don't accept their data in the same form, but everything appears to be working once I realized this. – Gene Callahan Feb 26 '15 at 19:10
  • Nicely put. I bet you could submit a suggested change to the docstrings. – cphlewis Feb 26 '15 at 21:19

0 Answers0