10000 Merge pull request #18719 from jirka-h/master · matplotlib/matplotlib@eaf4558 · GitHub
[go: up one dir, main page]

Skip to content

Commit eaf4558

Browse files
authored
Merge pull request #18719 from jirka-h/master
Added the trace plot of the end point
2 parents f2937c6 + 7011d1c commit eaf4558

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

examples/animation/double_pendulum.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import matplotlib.pyplot as plt
1515
import scipy.integrate as integrate
1616
import matplotlib.animation as animation
17+
from collections import deque
1718

1819
G = 9.8 # acceleration due to gravity, in m/s^2
1920
L1 = 1.0 # length of pendulum 1 in m
2021
L2 = 1.0 # length of pendulum 2 in m
22+
L = L1 + L2 # maximal length of the combined pendulum
2123
M1 = 1.0 # mass of pendulum 1 in kg
2224
M2 = 1.0 # mass of pendulum 2 in kg
2325
t_stop = 5 # how many seconds to simulate
26+
history_len = 500 # how many trajectory points to display
2427

2528

2629
def derivs(state, t):
@@ -47,8 +50,8 @@ def derivs(state, t):
4750

4851
return dydx
4952

50-
# create a time array from 0..100 sampled at 0.05 second steps
51-
dt = 0.05
53+
# create a time array from 0..t_stop sampled at 0.02 second steps
54+
dt = 0.02
5255
t = np.arange(0, t_stop, dt)
5356

5457
# th1 and th2 are the initial angles (degrees)
@@ -71,22 +74,32 @@ def derivs(state, t):
7174
y2 = -L2*cos(y[:, 2]) + y1
7275

7376
fig = plt.figure(figsize=(5, 4))
74-
ax = fig.add_subplot(autoscale_on=False, xlim=(-2, 2), ylim=(-2, 1))
77+
ax = fig.add_subplot(autoscale_on=False, xlim=(-L, L), ylim=(-L, 1.))
7578
ax.set_aspect('equal')
7679
ax.grid()
7780

7881
line, = ax.plot([], [], 'o-', lw=2)
82+
trace, = ax.plot([], [], ',-', lw=1)
7983
time_template = 'time = %.1fs'
8084
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
85+
history_x, history_y = deque(maxlen=history_len), deque(maxlen=history_len)
8186

8287

8388
def animate(i):
8489
thisx = [0, x1[i], x2[i]]
8590
thisy = [0, y1[i], y2[i]]
8691

92+
if i == 0:
93+
history_x.clear()
94+
history_y.clear()
95+
96+
history_x.appendleft(thisx[2])
97+
history_y.appendleft(thisy[2])
98+
8799
line.set_data(thisx, thisy)
100+
trace.set_data(history_x, history_y)
88101
time_text.set_text(time_template % (i*dt))
89-
return line, time_text
102+
return line, trace, time_text
90103

91104

92105
ani = animation.FuncAnimation(

0 commit comments

Comments
 (0)
0