Python 尝试在matplotlib中设置散点图动画
我得到了一个散点图。现在,我正试图将其动画化。我已经浏览了多个文档,了解如何做到这一点。我得到散点图的动画,但是没有一个点在正确的位置。我相信我误解了一些关于如何使用set_偏移的内容,但我不知道是什么 下面是调用matplotlib的类中的代码。它将代理设置在初始绘图的正确位置:Python 尝试在matplotlib中设置散点图动画,python,matplotlib,scatter-plot,Python,Matplotlib,Scatter Plot,我得到了一个散点图。现在,我正试图将其动画化。我已经浏览了多个文档,了解如何做到这一点。我得到散点图的动画,但是没有一个点在正确的位置。我相信我误解了一些关于如何使用set_偏移的内容,但我不知道是什么 下面是调用matplotlib的类中的代码。它将代理设置在初始绘图的正确位置: def plot(self): Show where agents are in graphical form. ------------------------
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
下面是我尝试制作这个情节的动画:
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)
动画开始后,点确实会四处移动,但正如我所提到的,它们都没有移动到正确的位置!正如我所说的,我想我把设置偏移量搞错了,但我不知道我是怎么做到的。你看到了吗?因为这似乎是可行的,而且看起来它使用set_偏移量的方式也不同。这是我举的一个例子。我以为我在使用set_of sets(),就像这个例子使用它的方式!(显然我错了,但我不明白的是我做错了什么。)你设置了非常非常简单的虚拟数据?如果列因某个或某个原因而无序或关闭?变体的数据类型和形状是什么?变体是一个字典(具有不同的代理类型),其中包含一个字典(“x”和“y”),其中x包含一个x位置列表,y包含一个y位置列表。