0

在 期间似乎不可能更改 y 轴和 x 轴视图限制ArtistAnimation,并以不同的轴限制重播帧。

限制似乎固定在调用动画函数之前最后设置的那些。

在下面的代码中,我有两个绘图阶段。第二个图中的输入数据是第一帧数据的一个小得多的子集。第一阶段的数据范围更广。

因此,在显示第二个图时,我需要“放大”(否则,如果轴限制保持不变,则图会非常小)。

这两个图叠加在两个不同的图像上(大小相同,但内容不同)。

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.image as mpimg

import random


# sample 640x480 image. Actual frame loops through 
# many different images, but of same size
image = mpimg.imread('image_demo.png')

fig = plt.figure()
plt.axis('off')

ax = fig.gca()

artists = []

def plot_stage_1():
  # both x, y axis limits automatically set to 0 - 100
  # when we call ax.imshow with this extent 
  im_extent = (0, 100, 0, 100) # (xmin, xmax, ymin, ymax)

  im = ax.imshow(image, extent=im_extent, animated=True)

  # y axis is a list of 100 random numbers between 0 and 100
  p, = ax.plot(range(100), random.choices(range(100), k=100))

  # Text label at 90, 90
  t = ax.text(im_extent[1]*0.9, im_extent[3]*0.9, "Frame 1")

  artists.append([im, t, p])


def plot_stage_2():
  # axes remain at the the 0 - 100 limit from the previous
  # imshow extent so both the background image and plot are tiny
  im_extent = (0, 10, 0, 10) 

  # so let's update the x, y axis limits
  ax.set_xlim(im_extent[0], im_extent[1])
  ax.set_ylim(im_extent[0], im_extent[3])

  im = ax.imshow(image, extent=im_extent, animated=True)

  p, = ax.plot(range(10), random.choices(range(10), k=10))

  # Text label at 9, 9
  t = ax.text(im_extent[1]*0.9, im_extent[3]*0.9, "Frame 2")

  artists.append([im, t, p])

plot_stage_1()
plot_stage_2()

# clear white space around plot
fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
# set figure size
fig.set_size_inches(6.67, 5.0, True)

anim = animation.ArtistAnimation(fig, artists, interval=2000, repeat=False, blit=False)

plt.show()

如果我只调用上述两个函数之一,则情节很好。但是,如果我同时调用两者,则两个帧中的轴限制将为 0 - 10、0 - 10。因此第 1 帧将被超级放大。

也调用ax.set_xlim(0, 100), ax.set_ylim(0, 100)inplot_stage_1()也无济于事。最后set_xlim()set_ylim()调用修复动画中所有帧的轴限制。

我可以保持轴边界固定并对输入数据应用缩放函数。

但是,我很想知道我是否可以简单地更改轴限制——这样我的代码会更好,因为实际代码很复杂,有多个阶段,在许多不同的范围内缩放图。

或者也许我必须重新调整我的代码才能使用FuncAnimation,而不是ArtistAnimation

4

1 回答 1

0

FuncAnimation似乎会导致预期的行为。所以我正在更改我的代码以使用它而不是ArtistAnimation.

不过仍然很想知道,这是否可以使用ArtistAnimation.

于 2020-04-09T05:37:23.650 回答