使用 matplotlib 动画更新 x 轴值 [英] Updating the x-axis values using matplotlib animation

查看:45
本文介绍了使用 matplotlib 动画更新 x 轴值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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.

任何帮助将不胜感激.

推荐答案

如果你不需要 blitting

If you don't need blitting

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 的边界框来玩游戏,以便更新轴标签.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆