Updating the x-axis values using matplotlib animat

2020-02-09 10:01发布

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.

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.

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.

Here is my code:

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()

I only want the second subplot (ax2) to update the x-axis values.

Any help would be much appreciated.

3条回答
趁早两清
2楼-- · 2020-02-09 10:40

This moves your axis, but is very slow.

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()
查看更多
欢心
3楼-- · 2020-02-09 10:44

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()

will work.

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.

查看更多
Summer. ? 凉城
4楼-- · 2020-02-09 10:49

If you are using blitting, you can call pyplot.draw() to redraw the entire figure, each time you change y/x axis.

This updates whole figure, so is relatively slow, but it's acceptable if you don't call it many items.

查看更多
登录 后发表回答