使用Matplotlib动画更新x轴值 [英] Updating the x-axis values using matplotlib animation
问题描述
我正在尝试使用matplotlib.ArtistAnimation
设置两个子图的动画.我希望x轴的值随着动画的进行而增加,以使动画的总长度为100,但是在任何时候,子图只向我显示0到24的时间值,然后迭代到100.
I am trying to use matplotlib.ArtistAnimation
to animate two subplots. I want the x-axis to increase in value as the animation progresses, such that the total length of the animation is 100 but at any time the subplot is only presenting me with the time values from 0-24 and then iterates up to 100.
此处给出了一个很好的例子.链接使用FuncAnimation
并使用plot().axes.set_xlim()
并递增x值以滚动方式更新x轴标签.可以通过提供的链接中的YouTube视频下方的链接获得该代码.
A great example is given here. The link uses FuncAnimation
and updates the x-axis labels in a rolling fashion using plot().axes.set_xlim()
and incrementing the x-values. The code is available via the link below the YouTube video in the link provided.
我在下面附加了代码,显示了我尝试复制这些结果的尝试,但x限制似乎采用其最终值,而不是随时间增加.我还尝试通过仅在子图中看到的窗口中绘制值来增加解决方案(相对于轴),但不会增加x轴值.我也尝试实现自动缩放,但是x轴仍然无法更新.
I have appended code below that shows my attempts to replicate these results but the x-limits seem to take on their final values instead of incrementing with time. I have also tried incrementing the solution (as opposed to the axis) by only plotting the values in the window that will be seen in the subplot, but that does not increment the x-axis values. I also tried to implement autoscaling but the x-axis still does not update.
我还发现了这个问题,实际上是相同的问题,但问题从未得到解决.
I also found this question which is virtually the same problem, but the question was never answered.
这是我的代码:
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轴值.
I only want the second subplot (ax2
) to update the x-axis values.
任何帮助将不胜感激.
推荐答案
如果您不需要需要发短信
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()
将起作用.
如果您需要运行得更快,则需要使用用于划线的边框玩游戏,以便更新轴标签.
If you need to run faster, you will need to play games with the bounding box used for blitting so that the axes labels are updated.
这篇关于使用Matplotlib动画更新x轴值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!