import numpy as np import matplotlib.pyplot as plt ## 1) setting up the plotting area plt.ion() # interactive is on fig = plt.figure(1, figsize=(7, 5)) plt.suptitle("A sequence of functions with unbounded norm", fontsize=13, c='b') axes = fig.add_subplot(111, aspect='equal') axes.set(xlim=(0, 1), ylim=(-.05, .85)) ## 2) defining the data to animate nbOfPoints = 800 N = 70 # number of functions X = np.linspace(0, 1, nbOfPoints) cst = 1.15 def F(x, n): return n**cst*x*np.exp(-n*x) Y_list = [] for n in range(1, N+1): Y_list.append(F(X, n)) ## 3) plotting the first functions curve = axes.plot(X, Y_list[1], color='blue', lw=1.15, label='current function')[0] curveR1 = axes.plot(X, Y_list[1], color='dodgerblue', lw=.75)[0] curveR1.set_alpha(.7) curveR0 = axes.plot(X, Y_list[0], color='dodgerblue', lw=.75)[0] curveR0.set_alpha(.4) axes.plot(X, F(X, 150), color='red', lw=1.15, label=f"function with n = {150}") axes.legend() """ We are setting up three polygonal line objects with some desired attributes: curve will be the current curve of the sequence (with color blue); curveR* will be the old current curve with a lesser opacity and a lesser line weight. Note the [0] at the end. This is necessary because the plot command returns a list of polygonal line objects. We need the zeroth entrance in this list. """ ## 4) looping for an elementary animation k = 1 for Y in Y_list[1:]: curveR0.set_ydata(curveR1.get_ydata()) curveR1.set_ydata(curve.get_ydata()) curve.set_ydata(Y) axes.set_title(f"n^{1.15} x exp(-nx) with index n = {k}") fig.show() plt.pause(0.2) k = k + 1 plt.show()