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.