Dec-30-2022, 09:45 PM
Hi,
I have a question about FuncAnimation of Matplotlib. Here is my code:
It start's without the line-plot. After 3s there is a line-plot. I don't know, why. I thought, it would start with the init() and after 3s it would call update().
I have a question about FuncAnimation of Matplotlib. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import matplotlib.animation def update(n): t = n * delta_t u = np.cos(k * x - omega * t) plot.set_ydata(u) text.set_text( f "t = {t:5.1f}" ) return plot, text, def init(): global plot global text plot, = ax.plot(x, np.cos(k * x)) text = ax.text( 0.0 , 1.05 , '') return plot, text, if __name__ = = '__main__' : x = np.linspace( 0 , 20 , 500 ) omega = 1.0 k = 1.0 delta_t = 0.02 fig = plt.figure() ax = fig.add_subplot( 1 , 1 , 1 ) ax.set_ylim( - 1.2 , 1.2 ) ax.set_xlabel( "Ort x" ) ax.set_ylabel( "u(x, t)" ) ax.grid() ani = mpl.animation.FuncAnimation(fig, update, interval = 3000 , blit = True , init_func = init) plt.show() |