我正在尝试编写一个程序,在其中为数学函数设置动画。不过,我想使用背景图像。我试过了,程序启动得很好,但很快它就会失去对图像的跟踪,一切都变得一团糟。我该如何解决这个问题?
我已经尝试实时调整图像大小以及 x 和 y 限制,但它仍然变得混乱。我正在使用 matplotlib 和 numpy。
此图像是在程序运行时:

但是,在这个程序中,程序存在错误:

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import numpy as np
#[...]
def animate(data): #refresh data shown
x,y = data
xdata.append(x)
ydata.append(y)
xmin,xmax = ax.get_xlim()
if x >= xmax:
ax.set_xlim(xmin,2*xmax)
ax.figure.canvas.draw()
line.set_data(xdata,ydata)
return line
img = plt.imread("myimage.jpg")
ax.imshow(img, extent=[0, ax.get_xlim()[-1], ax.get_ylim()[0], ax.get_ylim()[-1]])
ani = animation.FuncAnimation(fig,animate,data_gen,blit=True,interval=10,repeat = False)
plt.show()
(我试图将图像上传到这个问题,但直到现在我无法做到)
