我有2个numpy数组,其长度为n,分别对应于n次的x和y坐标(位置的n个快照)。
我尝试使用下面的代码。
%matplotlib notebook
# instead of matplotlib inline. It turns the notebook interactive
import matplotlib.pyplot as plt
from matplotlib import animation #
from IPython.display import display, Image
from IPython.display import HTML
import numpy as np
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure() # fig is a figure object
ax = plt.axes(xlim=(-1*10**8, 1*10**8), ylim=(-1*10**8,1*10**8)) # ax are the axes. They must be kept fixed in all plots
line, = ax.plot([], [], lw=1) # line holds the graph to nbe drawn in figure fig. Here it is a
# empty graph
# Initialization function: draws first frame on video. (empty box in this case)
def init():
line.set_data([], []) # the line has no data
return line,
#
# the data to plot
# i indexes the frames of the animation
# Animation function which updates figure data. This is called sequentially
def animate(i):
line.set_data(x1[i], y1[i]) # line graph of x and y coordinates
return line,
# Call the animator.
# Draws in Fig
# calls animate(i) for every i in frames
# delay between frames in miliseconds
# blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=range(len(x1)), interval=1000, blit=True)
plt.close(anim._fig)
# Call function to create and display the animation
#HTML(anim.to_html5_video())
HTML(anim.to_jshtml()) # empty graph
如果代码有效,我将看到x和y位置的动画。