当前位置: 首页 > 面试题库 >

使用Matplotlib动画更新x轴值

窦彦君
2023-03-14
问题内容

我正在尝试使用matplotlib.ArtistAnimation动画制作两个子图。我希望x轴的值随着动画的进行而增加,以使动画的总长度为100,但在任何时候,子图仅向我显示0到24的时间值,然后迭代到100。

这里有一个很好的例子。链接使用FuncAnimationplot().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 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的边框玩游戏,以便更新轴标签。



 类似资料:
  • 我想使用chartjs折线图来可视化我的数据点。默认情况下,Chartjs似乎会为图形设置动画,但不会为x轴上的值设置动画。x轴仅以离散步骤移动。 是否有任何方法也可以启用轴上的动画? 谢谢

  • 本文向大家介绍Python 用matplotlib画以时间日期为x轴的图像,包括了Python 用matplotlib画以时间日期为x轴的图像的使用技巧和注意事项,需要的朋友参考一下 1.效果展示 主要效果就是,x轴 显示时间单位。 下图展示的就是想要到达的效果。 其实主要是运用了datetime.date这个类型的变量作为x轴坐标的数据输入。 2. 源码 将data.txt中的数据读入,用mat

  • 问题内容: 我想用matplotlib制作3D动画,但是我不知道该怎么做。这是我的无效代码。 问题答案: 我使用了以下示例http://matplotlib.org/1.4.1/examples/animation/simple_3danim.html 并修改了您的代码:

  • 我有以下熊猫数据帧: 使用Matplotlib绘制线图时,x轴(日期)都压在一起。如下所示: 你知道这是怎么做到的吗?我对此比较陌生,使用过的代码如下: 多谢

  • 我创建了一个图,它显示了一组数据和直方图。让我困扰的是,如下所示,直方图上的X轴有一个20步,因此最后一个值是140,而不是150,这严重触发了我的强迫症。有人能帮我修理它吗? 生成的png文件: https://i.stack.imgur.com/NhBYM.png 以及守则的有关部分: 非常感谢。

  • 问题内容: 我有这个代码。我想添加一个子图来绘制余弦函数。(我不想创建一个类)。第二个图也应动态更新 问题答案: 基本上,您可以使用与示例中的结构非常相似的结构。您只需要创建一个附加轴(子图)和第二条线对象: