Python Matplotlib Basemap Hexbin动画:清除帧之间的hexbins
我正在底图图像上制作一个hexbin热图的动画,但是在开始下一帧之前,我不知道如何删除之前的hex。我想用一些累加设置动画,但在选定的时间间隔重置(我的数据有一个时间维度,我想逐日显示给定年份的所有点,然后擦除十六进制并显示下一年)。我想我需要存储层,并使用它的.remove()方法,但我想不出来Python Matplotlib Basemap Hexbin动画:清除帧之间的hexbins,python,matplotlib,matplotlib-basemap,Python,Matplotlib,Matplotlib Basemap,我正在底图图像上制作一个hexbin热图的动画,但是在开始下一帧之前,我不知道如何删除之前的hex。我想用一些累加设置动画,但在选定的时间间隔重置(我的数据有一个时间维度,我想逐日显示给定年份的所有点,然后擦除十六进制并显示下一年)。我想我需要存储层,并使用它的.remove()方法,但我想不出来 def update_hex(i, prev_layer): if i == (len(years) - 1): ani.event_source.stop() print("En
def update_hex(i, prev_layer):
if i == (len(years) - 1):
ani.event_source.stop()
print("End animation: Update Hex")
x, y = [mm.xmin, mm.xmax], [mm.ymin, mm.ymax]
x1, y1 = mm(df["LONGITUDE"][df["YEAR"] == years[i]].values, df["LATITUDE"][df["YEAR"] == years[i]].values)
x += x1.tolist()
y += y1.tolist()
x = np.array(x)
y = np.array(y)
if prev_layer:
prev_layer.remove()
hexlayer = mm.hexbin(x, y, gridsize = 75, bins = 100, mincnt = 0, cmap = 'my_cmap', linewidth = 0)
prev_layer = None
ani = animation.FuncAnimation(fig, update_hex, interval = 10, frames = range(len(years)), fargs = (prev_layer, ))
plt.show()
在本例中,我将使用一个全局数组来存储由
hexbin()
返回的对象。在预定义的时间间隔内,我会删除这些对象,清空数组的内容,然后重复
fig, ax = plt.subplots()
def animate(i):
x0,y0 = np.random.random(size=(2,))*4-2
x = np.random.normal(loc=x0, size=(1000,))
y = np.random.normal(loc=y0, size=(1000,))
if len(prevlayers)>=maxlayers:
for layer in prevlayers:
layer.remove()
prevlayers[:] = []
hexlayer = ax.hexbin(x,y, gridsize=10, alpha=0.5)
prevlayers.append(hexlayer)
return hexlayer,
maxlayers = 3
prevlayers = []
ani = matplotlib.animation.FuncAnimation(fig, animate, frames=12)
我尝试在maxlayers=1的情况下使用此解决方案,但出现了一个错误
ValueError:list.remove(x):x不在列表中
。有没有更好的方法一次只显示一个图层?