具有活页夹的Jupyter笔记本中的慢速动画

时间:2019-12-13 00:17:36

标签: python animation jupyter-notebook android-binder

我正在尝试在Jupyter笔记本中制作一些简单的动画,我希望稍后与Binder分享。但是,尽管我的计算机上一切正常,但使用Binder并非如此。以下是带有旋转线的示例:Binder link。当单击“播放”按钮时,我没有规律地旋转线条,而是经常打h;手动移动光标时也是如此。为什么这么简单呢?请注意,对于这个特别简单的示例,结果可能没问题,但是一旦我添加更多条件和事件,最终结果基本上就无法使用。

我欢迎任何解释或解决方案;)。

编辑:如果您不想单击链接,则为以下代码:

import bqplot.pyplot as pl
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from ipywidgets import Accordion, IntSlider, FloatSlider, HTMLMath, Dropdown, Box, HBox, VBox, Layout, Play, jslink
from IPython.display import display
import math
import numpy as np
plot_rotation = pl.figure()
pl.clear()
# Define center point
pt = np.array([0,0])
# Plot a horizontal line
line = pl.plot([pt[0]-1,pt[0]+1], [pt[1],pt[1]], lw=1.5, colors = ["#6699cc"])
pl.xlim(-1,1)
pl.ylim(-1,1)
# Define function that will rotate the line, given a specific angle
def animate_line(rot_deg):
    global pt
    dangle = 0.1 # Incremental angle, in degrees
    # We use polar coordinates
    radius = 1    
    theta = math.radians(rot_deg*dangle) # Converts to radians
    # Define coordinates.
    x = radius*math.cos(theta)
    y = radius*math.sin(theta)
    # Create symmetric point with respect to center
    x2 = -radius*math.cos(theta)
    y2 = -radius*math.sin(theta)
    # Update line coordinates 
    line.x = [x2,x]
    line.y = [y2,y]
    return line
angle_widget = widgets.IntSlider(min=0, max=7200, step=10, value=0, description = "$\\theta$*10", continuous_update=True)
play_button_angle = Play(min=0, max=7200, step=5, interval=10)
jslink((play_button_angle, 'value'), (angle_widget, 'value'))
# Define function to be called when the angle is changed
def on_change_rot(change):
    _ = animate_line(rot_deg=angle_widget.value)

angle_widget.observe(on_change_rot, names='value', type='change')
display(Box([
        HBox([play_button_angle,angle_widget], layout=Layout(justify_content='center',width='905px'))],
        layout=Layout(width='100%', flex_flow='row wrap', display='flex')))
display(HBox([plot_rotation]),layout=Layout(justify_content='center',width='905px'))

0 个答案:

没有答案