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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation
phi = np.linspace(0, 2 * np.pi, 40, endpoint = True) particles = np.array([[np.cos(phi)], [np.sin(phi)]])
def position(frame, model = 0, h0 = 0.3): time = frame if model == 0: pos = particles * (1 + 0.5 * h0 * np.sin(time) * np.cos(2 * phi)) else: pos = particles * (1 + 0.5 * h0 * np.sin(time) * np.sin(2 * phi)) ln.set_data(pos[0], pos[1]) return ln,
def init(): ax.set_xlim(-1.5, 1.5) ax.set_ylim(-1.5, 1.5) ax.set_xlabel('x') ax.set_ylabel('y') return ln,
fig, ax = plt.subplots(figsize = (6, 6)) plt.plot(particles[0], particles[1], 'bo', markersize = 2) ln, = plt.plot([], [], 'ro', markersize = 3) plt.title('$h_+$ when $t = \pi/2$', fontsize = 14) init() position(np.pi * 0.5) plt.savefig('1.jpg') plt.show()
fig, ax = plt.subplots(figsize = (6, 6)) plt.plot(particles[0], particles[1], 'bo', markersize = 2) ln, = plt.plot([], [], 'ro', markersize = 3) init() plt.title('$h_+$ when $t = -\pi/2$', fontsize = 14) position(-np.pi * 0.5) plt.savefig('2.jpg') plt.show()
fig, ax = plt.subplots(figsize = (6, 6)) plt.plot(particles[0], particles[1], 'bo', markersize = 2) ln, = plt.plot([], [], 'ro', markersize = 3) init() plt.title(r'$h_\times$ when $t = \pi/2$', fontsize = 14) position(np.pi * 0.5, 1) plt.savefig('3.jpg') plt.show()
fig, ax = plt.subplots(figsize = (6, 6)) plt.plot(particles[0], particles[1], 'bo', markersize = 2) ln, = plt.plot([], [], 'ro', markersize = 3) init() plt.title(r'$h_\times$ when $t = -\pi/2$', fontsize = 14) position(-np.pi * 0.5, 1) plt.savefig('4.jpg') plt.show()
fig, ax = plt.subplots(figsize = (6, 6)) plt.plot(particles[0], particles[1], 'bo', markersize = 2) ln, = plt.plot([], [], 'ro', markersize = 3, animated=True) plt.title(r'$h_\times$') anim = animation.FuncAnimation(fig, position, frames = np.linspace(0, 2*np.pi, 90), fargs = (1, 0.3), init_func=init, interval=50, blit=True) anim.save('hx.gif', writer = animation.writers['pillow'](fps = 30))
fig, ax = plt.subplots(figsize = (6, 6)) plt.plot(particles[0], particles[1], 'bo', markersize = 2) ln, = plt.plot([], [], 'ro', markersize = 3, animated=True) plt.title('$h_+$') anim = animation.FuncAnimation(fig, position, frames = np.linspace(0, 2*np.pi, 90), fargs = (0, 0.3), init_func=init, interval=50, blit=True) anim.save('h+.gif', writer = animation.writers['pillow'](fps = 30))
|