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

时间:2018-04-06 03:37:07

标签: python animation google-colaboratory

我有一个点圈的动画,从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

我要么得到一个尾随的圆圈,其中前一帧不会被删除(以及角落中的另一个随机副本): enter image description here

或者,如果我取消注释plt.close(),我只会得到第一帧,那就是它......

enter image description here

为什么要嘲笑我......

1 个答案:

答案 0 :(得分:1)

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

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