abr*_*add 10 python animation matplotlib
我试图用来matplotlib.ArtistAnimation动画两个子图.我希望随着动画的进行,x轴的值增加,这样动画的总长度就是100,但是在任何时候,子图只向我显示0-24的时间值,然后迭代到100.
这里给出了一个很好的例子.该链接使用FuncAnimation并plot().axes.set_xlim()递增x值以滚动方式使用和更新x轴标签.该代码可通过所提供链接中YouTube视频下方的链接获取.
我在下面附加了代码,显示了我尝试复制这些结果但x限制似乎采用了它们的最终值而不是随时间递增.我还尝试通过仅绘制将在子图中看到的窗口中的值来递增解(而不是轴),但不会增加x轴值.我也试图实现自动缩放,但x轴仍然没有更新.
我也发现这个问题实际上是同一个问题,但问题从来没有得到解答.
这是我的代码:
import matplotlib.pylab as plt
import matplotlib.animation as anim
import numpy as np
#create image with format (time,x,y)
image = np.random.rand(100,10,10)
#setup figure
fig = plt.figure()
ax1=fig.add_subplot(1,2,1)
ax2=fig.add_subplot(1,2,2)
#set up viewing window (in this case the 25 most recent values)
repeat_length = (np.shape(image)[0]+1)/4
ax2.set_xlim([0,repeat_length])
#ax2.autoscale_view()
ax2.set_ylim([np.amin(image[:,5,5]),np.amax(image[:,5,5])])
#set up list of images for animation
ims=[]
for time in xrange(np.shape(image)[0]):
im = ax1.imshow(image[time,:,:])
im2, = ax2.plot(image[0:time,5,5],color=(0,0,1))
if time>repeat_length:
lim = ax2.set_xlim(time-repeat_length,time)
ims.append([im, im2])
#run animation
ani = anim.ArtistAnimation(fig,ims, interval=50,blit=False)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我只希望第二个子图(ax2)更新x轴值.
任何帮助将非常感激.
如果你没有需要阻击器
import matplotlib.pylab as plt
import matplotlib.animation as animation
import numpy as np
#create image with format (time,x,y)
image = np.random.rand(100,10,10)
#setup figure
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
#set up viewing window (in this case the 25 most recent values)
repeat_length = (np.shape(image)[0]+1)/4
ax2.set_xlim([0,repeat_length])
#ax2.autoscale_view()
ax2.set_ylim([np.amin(image[:,5,5]),np.amax(image[:,5,5])])
#set up list of images for animation
im = ax1.imshow(image[0,:,:])
im2, = ax2.plot([], [], color=(0,0,1))
def func(n):
im.set_data(image[n,:,:])
im2.set_xdata(np.arange(n))
im2.set_ydata(image[0:n, 5, 5])
if n>repeat_length:
lim = ax2.set_xlim(n-repeat_length, n)
else:
# makes it look ok when the animation loops
lim = ax2.set_xlim(0, repeat_length)
return im, im2
ani = animation.FuncAnimation(fig, func, frames=image.shape[0], interval=30, blit=False)
plt.show()
Run Code Online (Sandbox Code Playgroud)
将工作.
如果您需要更快地运行,则需要使用用于blitting的边界框来玩游戏,以便更新轴标签.
| 归档时间: |
|
| 查看次数: |
9062 次 |
| 最近记录: |