Skip to main content
 首页 » 编程设计

python之需要帮助在 Google Colab 中运行 Python 代码

2025年01月19日11txw1958

我有一个点从 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(),我只会得到第一帧,仅此而已...

为什么要 mock 我....

请您参考如下方法:

我稍微修改了您的代码,现在它产生了所需的输出。

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 向量,但这已经是一个不错的动画。