-
Notifications
You must be signed in to change notification settings - Fork 93
/
plotting.py
66 lines (53 loc) · 2.2 KB
/
plotting.py
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
# Generalized Lagrangian Networks | 2020
# Miles Cranmer, Sam Greydanus, Stephan Hoyer (...)
import numpy as np # get rid of this eventually
from functools import partial # reduces arguments to function by making some subset implicit
# visualization
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from moviepy.editor import ImageSequenceClip
from functools import partial
import proglog
from PIL import Image
from .utils import radial2cartesian
def plot_dblpend(ax, i, cart_coords, l1, l2, max_trail=30, trail_segments=20, r = 0.05):
# Plot and save an image of the double pendulum configuration for time step i.
plt.cla()
x1, y1, x2, y2 = cart_coords
ax.plot([0, x1[i], x2[i]], [0, y1[i], y2[i]], lw=2, c='k') # rods
c0 = Circle((0, 0), r/2, fc='k', zorder=10) # anchor point
c1 = Circle((x1[i], y1[i]), r, fc='b', ec='b', zorder=10) # mass 1
c2 = Circle((x2[i], y2[i]), r, fc='r', ec='r', zorder=10) # mass 2
ax.add_patch(c0)
ax.add_patch(c1)
ax.add_patch(c2)
# plot the pendulum trail (ns = number of segments)
s = max_trail // trail_segments
for j in range(trail_segments):
imin = i - (trail_segments-j)*s
if imin < 0: continue
imax = imin + s + 1
alpha = (j/trail_segments)**2 # fade the trail into alpha
ax.plot(x2[imin:imax], y2[imin:imax], c='r', solid_capstyle='butt',
lw=2, alpha=alpha)
# Center the image on the fixed anchor point. Make axes equal.
ax.set_xlim(-l1-l2-r, l1+l2+r)
ax.set_ylim(-l1-l2-r, l1+l2+r)
ax.set_aspect('equal', adjustable='box')
plt.axis('off')
def fig2image(fig):
fig.canvas.draw()
data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
image = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
return image
def get_dblpend_images(y, fig, ax, l1=1, l2=1, verbose=True):
theta1, theta2 = y[:, 0], y[:, 1]
cart_coords = radial2cartesian(theta1, theta2, l1, l2)
images = [] ; di = 1
N = len(y)
for i in range(0, N, di):
if verbose:
print("{}/{}".format(i // di, N // di), end='\n' if i//di%15==0 else ' ')
plot_dblpend(ax, i, cart_coords, l1, l2)
images.append( fig2image(fig) )
return images