# example code taken from https://courspython.com/animation-matplotlib.html import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def runAnimation(x, t, data, xlabel='x', data_label='y', title="", repeat=False): """ Run a matplotlib 2D animation :param x: the abscisses array of size nx :param t: the times array of size nt :param data: the data to display, must be an array of size [nt,nx] :param xlabel: the string to set abscisse label :param data_label: the string to set label on ordinates :param title: the title of the graph :param repeat: a boolean to specify if the animation indefenitely repeats or not """ # figure initialization fig, ax = plt.subplots() line, = ax.plot([],[]) # set limits ax.set_xlim(x[0],x[-1]) ax.set_ylim(np.min(data),np.max(data)) # set ax labels and title ax.set_xlabel(xlabel) ax.set_ylabel(data_label) ax.set_title(title) # function to define when blit=True # creates the background of the animation which will be on each image def init(): line.set_data([],[]) return line, # update line value def animate(i): line.set_data(x, data[i]) label = f"t={t[i]:.2f}s." line.set_label(label) ax.legend().remove() return line, ax.legend(loc='upper right') # run the animation ani = animation.FuncAnimation(fig, animate, init_func=init, frames=100, blit=True, interval=20, repeat=repeat) plt.show() return None