Python 使用matplotlib动画更新x轴值
我正在尝试使用Python 使用matplotlib动画更新x轴值,python,animation,matplotlib,Python,Animation,Matplotlib,我正在尝试使用matplotlib.ArtistAnimation为两个子情节设置动画。我希望x轴的值随着动画的进行而增加,这样动画的总长度为100,但在任何时候,子图仅向我显示0-24之间的时间值,然后迭代到100 给出了一个很好的例子。该链接使用FuncAnimation,并使用plot().axes.set_xlim()并递增x轴值,以滚动方式更新x轴标签。代码可通过YouTube视频下方提供的链接获得 我在下面添加了代码,显示了我复制这些结果的尝试,但x限制似乎是以它们的最终值为准,而不
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()
我只希望第二个子批(ax2
)更新x轴值
任何帮助都将不胜感激。这会移动你的轴,但速度非常慢
import matplotlib.pylab as plt
import matplotlib.animation as anim
import numpy as np
image = np.random.rand(100,10,10)
repeat_length = (np.shape(image)[0]+1)/4
fig = plt.figure()
ax1 = ax1=fig.add_subplot(1,2,1)
im = ax1.imshow(image[0,:,:])
ax2 = plt.subplot(122)
ax2.set_xlim([0,repeat_length])
ax2.set_ylim([np.amin(image[:,5,5]),np.amax(image[:,5,5])])
im2, = ax2.plot(image[0:0,5,5],color=(0,0,1))
canvas = ax2.figure.canvas
def init():
im = ax1.imshow(image[0,:,:])
im2.set_data([], [])
return im,im2,
def animate(time):
time = time%len(image)
im = ax1.imshow(image[time,:,:])
im2, = ax2.plot(image[0:time,5,5],color=(0,0,1))
if time>repeat_length:
print time
im2.axes.set_xlim(time-repeat_length,time)
plt.draw()
return im,im2,
ax2.get_yaxis().set_animated(True)
# call the animator. blit=True means only re-draw the parts that have changed.
animate = anim.FuncAnimation(fig, animate, init_func=init,
interval=0, blit=True, repeat=True)
plt.show()
如果你不需要闪电
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()
会有用的
如果您需要运行得更快,则需要使用用于blitting的边界框进行游戏,以便更新轴标签。如果您使用blitting,每次更改y/x轴时,可以调用
pyplot.draw()
重新绘制整个图形
这会更新整个图形,因此速度相对较慢,但如果您不将其称为多个项目,这是可以接受的。您不需要每次都重新创建
imshow
对象,而是使用set\u data
。当我使用blit时,动画不会运行,还不确定我为什么希望使用ArtistAnimation运行动画,但最终还是咬紧牙关,重新编写了代码。它似乎运行顺利。