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 40 41 42 43 44 45 46 47 48 49 50
|
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
T = 0.5
Lambda = 4
k = 2*np.pi/Lambda
w = 2*np.pi/T
dt = 0.004
xminbis = 0
xmaxbis = 16
nbx = 48
xbis = np.linspace(xminbis, xmaxbis, nbx)
pause = False
def simData():
t_max = 15.0
x = 0.0
t = 0.0
while t < t_max:
if not pause:
line.set_data([],[])
t = t + dt
yield t, line,
def onClick(event):
global pause
pause ^= True
def simPoints(simData):
t = simData[0]
y = 0.6*np.cos(k*xbis - w*t)
line.set_data(xbis, y)
time_text.set_text(time_template%(t))
return line, time_text
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], 'bo', ms=10)
ax.set_ylim(-1, 1)
ax.set_xlim(xminbis,xmaxbis)
plt.xlabel('X (m)', fontsize=16)
plt.ylabel('Y',fontsize=16,rotation = 'horizontal')
time_template = 'Time = %.1f s'
time_text = ax.text(0.05, 1.1, '', transform=ax.transAxes)
fig.canvas.mpl_connect('button_press_event', onClick)
ani = animation.FuncAnimation(fig, simPoints,simData, blit=False, interval=20,repeat=True)
fig.show() |
Partager