【发布时间】:2018-03-26 22:20:30
【问题描述】:
我正在尝试添加一个图例来覆盖显示随机数动画数组的 imshow() 图。我希望更新图例以显示我们正在查看的步骤。
我尝试按照步骤here 进行操作,该步骤展示了如何使用 FuncAnimation 为 subplots() 创建动画图例。我相信显示动画数组的唯一方法是使用 ArtistAnimation() 和 imshow(),但其中一个或两个导致我无法遵循链接的解决方案。
我在工作代码下方附加了生成动画随机数组,图例解决方案(来自链接)双注释掉。
任何帮助或补救建议将不胜感激。
谢谢, C
import matplotlib.animation as animation
from matplotlib import colors
import matplotlib.pyplot as plt
import numpy as np
N=20
steps = 100
interval_pause = 100
repeat_pause = 1000
cmap = colors.ListedColormap(['white', 'black'])
bounds=[-1,0,1]
norm = colors.BoundaryNorm(bounds, cmap.N)
fig = plt.figure()
ax = plt.gca()
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
ax.axes.xaxis.set_ticks([])
ax.axes.yaxis.set_ticks([])
#plt.colorbar(img, cmap=cmap, norm=norm, boundaries=bounds, ticks=[-1,0,1])
array = 2*(np.random.rand(N,N,steps)-0.5)
state = np.zeros(steps)
ims = []
##leg = ax.legend(loc='upper left',prop={'size':12})
for step in range(0,steps):
state = array[:,:,step]
im = plt.imshow(state,interpolation='nearest',cmap=cmap,norm=norm, animated=True)
##lab = 'step = '+str(step)
##leg.texts.set_text(lab)
ims.append([im])##+leg])
ani = animation.ArtistAnimation(fig,ims,interval=interval_pause,repeat_delay=repeat_pause)
#ani.save('animate_evolution '+str(timer())+'.mp4')
plt.show()
【问题讨论】:
标签: python python-3.x animation matplotlib imshow