代码拉取完成,页面将自动刷新
# 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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。