在 Google Colab 中需要帮助 运行 Python 代码
Need helping running Python code in Google Colab
我有一个点从 0 滚动到 15 并返回的动画。
我把它写在 PyCharm 里面,在那里工作得很好。但是,我一辈子都想不出如何让它在 Google Colab 中工作。我使用 these 指令来尝试让它工作。在过去的 6 个小时里,我真的花了它,我的大脑快要从我的耳朵里融化了......
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import animation, rc
from IPython.display import HTML
p = np.linspace(-np.pi/2,np.pi/2,10)
x = np.sin(p)
v = np.column_stack((np.concatenate((x,x)),np.concatenate((np.cos(p),-np.cos(p)))))
v = np.column_stack((v,[1]*len(v[::,0:1:])))
def R(theta):
return np.matrix([[np.cos(theta), -np.sin(theta), 0],[np.sin(theta), np.cos(theta), 0], [0,0,1]])
def T(dx, dy):
return np.matrix([[1,0,dx],[0,1,dy],[0, 0, 1]])
x, y, z = v[::,0:1:], v[::,1:2:], v[::,2:3:]
x = [i for s in x for i in s]
y = [i for s in y for i in s]
z = [i for s in z for i in s]
z0 = [x,y,z]
ln, = plt.plot(x, y, 'ro', animated=True)
plt.close()
fig, ax = plt.subplots()
ax.set_aspect(1)
def init():
ax.set_xlim(0, 15)
ax.set_ylim(-1, 3)
return ln,
def update(t):
if t <=70 :
z1 = T(t/5,1) * R(np.pi/180*((t*30)%360)) * z0
else:
z1 = T((140-t)/5,1) * R(-np.pi/180*((t*30)%360)) * z0
ln, = plt.plot(z1.tolist()[0], z1.tolist()[1], 'ro', animated=True)
# plt.close()
return ln,
ani = FuncAnimation(fig, update, frames= 140, init_func=init, interval=30, blit=True)
# Initialize the Animation object again
ani = FuncAnimation(fig, update, init_func=init, frames=140, interval=30, blit=True)
rc('animation', html='jshtml')
ani
我要么得到一个尾部圆圈,其中前几帧没有被删除(角落里有另一个随机副本):
或者,如果我取消注释 plt.close()
,我只得到第一帧,仅此而已...
为什么要嘲笑我....
我稍微修改了您的代码,现在它产生了所需的输出。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
p = np.linspace(-np.pi/2,np.pi/2,10)
x = np.sin(p)
v = np.column_stack((np.concatenate((x,x)),np.concatenate((np.cos(p),-np.cos(p)))))
v = np.column_stack((v,[1]*len(v[::,0:1:])))
def R(theta):
return np.matrix([[np.cos(theta), -np.sin(theta), 0],[np.sin(theta), np.cos(theta), 0], [0,0,1]])
def T(dx, dy):
return np.matrix([[1,0,dx],[0,1,dy],[0, 0, 1]])
x, y, z = v[::,0:1:], v[::,1:2:], v[::,2:3:]
x = [i for s in x for i in s]
y = [i for s in y for i in s]
z = [i for s in z for i in s]
z0 = [x,y,z]
fig, ax = plt.subplots()
plt.close()
ln, = ax.plot(x, y, 'ro', animated=True)
ax.set_aspect(1)
def init():
ax.set_xlim(0, 15)
ax.set_ylim(-1, 3)
return ln,
def update(t):
if t <=70 :
z1 = T(t/5,1) * R(np.pi/180*((t*30)%360)) * z0
else:
z1 = T((140-t)/5,1) * R(-np.pi/180*((t*30)%360)) * z0
ln.set_data(z1.tolist()[0], z1.tolist()[1])
return ln,
# Initialize the Animation object again
ani = animation.FuncAnimation(fig, update, init_func=init, frames=140,
interval=30, blit=True)
rc('animation', html='jshtml')
ani
您可能希望避免使用列表并使用所有 numpy 向量,但这已经是一个不错的动画。
我有一个点从 0 滚动到 15 并返回的动画。 我把它写在 PyCharm 里面,在那里工作得很好。但是,我一辈子都想不出如何让它在 Google Colab 中工作。我使用 these 指令来尝试让它工作。在过去的 6 个小时里,我真的花了它,我的大脑快要从我的耳朵里融化了......
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib import animation, rc
from IPython.display import HTML
p = np.linspace(-np.pi/2,np.pi/2,10)
x = np.sin(p)
v = np.column_stack((np.concatenate((x,x)),np.concatenate((np.cos(p),-np.cos(p)))))
v = np.column_stack((v,[1]*len(v[::,0:1:])))
def R(theta):
return np.matrix([[np.cos(theta), -np.sin(theta), 0],[np.sin(theta), np.cos(theta), 0], [0,0,1]])
def T(dx, dy):
return np.matrix([[1,0,dx],[0,1,dy],[0, 0, 1]])
x, y, z = v[::,0:1:], v[::,1:2:], v[::,2:3:]
x = [i for s in x for i in s]
y = [i for s in y for i in s]
z = [i for s in z for i in s]
z0 = [x,y,z]
ln, = plt.plot(x, y, 'ro', animated=True)
plt.close()
fig, ax = plt.subplots()
ax.set_aspect(1)
def init():
ax.set_xlim(0, 15)
ax.set_ylim(-1, 3)
return ln,
def update(t):
if t <=70 :
z1 = T(t/5,1) * R(np.pi/180*((t*30)%360)) * z0
else:
z1 = T((140-t)/5,1) * R(-np.pi/180*((t*30)%360)) * z0
ln, = plt.plot(z1.tolist()[0], z1.tolist()[1], 'ro', animated=True)
# plt.close()
return ln,
ani = FuncAnimation(fig, update, frames= 140, init_func=init, interval=30, blit=True)
# Initialize the Animation object again
ani = FuncAnimation(fig, update, init_func=init, frames=140, interval=30, blit=True)
rc('animation', html='jshtml')
ani
我要么得到一个尾部圆圈,其中前几帧没有被删除(角落里有另一个随机副本):
或者,如果我取消注释 plt.close()
,我只得到第一帧,仅此而已...
为什么要嘲笑我....
我稍微修改了您的代码,现在它产生了所需的输出。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
p = np.linspace(-np.pi/2,np.pi/2,10)
x = np.sin(p)
v = np.column_stack((np.concatenate((x,x)),np.concatenate((np.cos(p),-np.cos(p)))))
v = np.column_stack((v,[1]*len(v[::,0:1:])))
def R(theta):
return np.matrix([[np.cos(theta), -np.sin(theta), 0],[np.sin(theta), np.cos(theta), 0], [0,0,1]])
def T(dx, dy):
return np.matrix([[1,0,dx],[0,1,dy],[0, 0, 1]])
x, y, z = v[::,0:1:], v[::,1:2:], v[::,2:3:]
x = [i for s in x for i in s]
y = [i for s in y for i in s]
z = [i for s in z for i in s]
z0 = [x,y,z]
fig, ax = plt.subplots()
plt.close()
ln, = ax.plot(x, y, 'ro', animated=True)
ax.set_aspect(1)
def init():
ax.set_xlim(0, 15)
ax.set_ylim(-1, 3)
return ln,
def update(t):
if t <=70 :
z1 = T(t/5,1) * R(np.pi/180*((t*30)%360)) * z0
else:
z1 = T((140-t)/5,1) * R(-np.pi/180*((t*30)%360)) * z0
ln.set_data(z1.tolist()[0], z1.tolist()[1])
return ln,
# Initialize the Animation object again
ani = animation.FuncAnimation(fig, update, init_func=init, frames=140,
interval=30, blit=True)
rc('animation', html='jshtml')
ani
您可能希望避免使用列表并使用所有 numpy 向量,但这已经是一个不错的动画。