In [1]:
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, FloatSlider
In [2]:
t = np.linspace(0.0, 5.0)
t
Out[2]:
array([0.        , 0.10204082, 0.20408163, 0.30612245, 0.40816327,
       0.51020408, 0.6122449 , 0.71428571, 0.81632653, 0.91836735,
       1.02040816, 1.12244898, 1.2244898 , 1.32653061, 1.42857143,
       1.53061224, 1.63265306, 1.73469388, 1.83673469, 1.93877551,
       2.04081633, 2.14285714, 2.24489796, 2.34693878, 2.44897959,
       2.55102041, 2.65306122, 2.75510204, 2.85714286, 2.95918367,
       3.06122449, 3.16326531, 3.26530612, 3.36734694, 3.46938776,
       3.57142857, 3.67346939, 3.7755102 , 3.87755102, 3.97959184,
       4.08163265, 4.18367347, 4.28571429, 4.3877551 , 4.48979592,
       4.59183673, 4.69387755, 4.79591837, 4.89795918, 5.        ])
In [3]:
A = 0.5
In [4]:
y = A*np.sin(t)
y
Out[4]:
array([ 0.        ,  0.05093191,  0.10133397,  0.15068182,  0.19846207,
        0.24417767,  0.28735302,  0.32753895,  0.36431739,  0.39730573,
        0.42616078,  0.45058236,  0.47031639,  0.48515758,  0.49495154,
        0.49959636,  0.49904374,  0.49329942,  0.48242315,  0.4665281 ,
        0.44577962,  0.42039355,  0.39063401,  0.35681059,  0.31927516,
        0.27841822,  0.23466481,  0.18847011,  0.1403147 ,  0.09069956,
        0.04014084, -0.01083548, -0.06169907, -0.11192079, -0.16097816,
       -0.20836083, -0.25357585, -0.29615286, -0.3356489 , -0.37165308,
       -0.40379085, -0.43172786, -0.45517347, -0.47388378, -0.48766414,
       -0.49637119, -0.49991433, -0.49825672, -0.4914156 , -0.47946214])
In [5]:
%matplotlib widget
In [ ]:
 
In [6]:
fig, ax = plt.subplots()
In [7]:
lines = ax.plot(t, y)
In [8]:
lines
Out[8]:
[<matplotlib.lines.Line2D at 0x7f772abd5f98>]
In [9]:
type(lines)
Out[9]:
list
In [10]:
line = lines[0]
line
Out[10]:
<matplotlib.lines.Line2D at 0x7f772abd5f98>
In [11]:
[1, 2, 3]
Out[11]:
[1, 2, 3]
In [12]:
def update_line(A):
    y = A*np.sin(t)
    return y
In [13]:
update_line(2.0)
Out[13]:
array([ 0.        ,  0.20372766,  0.40533587,  0.60272726,  0.7938483 ,
        0.9767107 ,  1.14941208,  1.31015579,  1.45726957,  1.58922293,
        1.70464314,  1.80232944,  1.88126557,  1.94063034,  1.97980615,
        1.99838546,  1.99617496,  1.97319767,  1.92969262,  1.8661124 ,
        1.78311846,  1.68157421,  1.56253605,  1.42724236,  1.27710064,
        1.11367287,  0.93865923,  0.75388043,  0.5612588 ,  0.36279822,
        0.16056335, -0.04334191, -0.24679627, -0.44768314, -0.64391263,
       -0.8334433 , -1.01430342, -1.18461144, -1.34259559, -1.48661232,
       -1.61516338, -1.72691142, -1.82069389, -1.89553514, -1.95065657,
       -1.98548475, -1.99965734, -1.9930269 , -1.96566241, -1.91784855])
In [14]:
def update_line(A=0.5):
    y = A*np.sin(t)
    return y
In [15]:
update_line(A=1.0)
Out[15]:
array([ 0.        ,  0.10186383,  0.20266794,  0.30136363,  0.39692415,
        0.48835535,  0.57470604,  0.6550779 ,  0.72863478,  0.79461147,
        0.85232157,  0.90116472,  0.94063279,  0.97031517,  0.98990308,
        0.99919273,  0.99808748,  0.98659884,  0.96484631,  0.9330562 ,
        0.89155923,  0.84078711,  0.78126802,  0.71362118,  0.63855032,
        0.55683643,  0.46932961,  0.37694022,  0.2806294 ,  0.18139911,
        0.08028167, -0.02167096, -0.12339814, -0.22384157, -0.32195632,
       -0.41672165, -0.50715171, -0.59230572, -0.67129779, -0.74330616,
       -0.80758169, -0.86345571, -0.91034694, -0.94776757, -0.97532829,
       -0.99274237, -0.99982867, -0.99651345, -0.9828312 , -0.95892427])
In [16]:
update_line()
Out[16]:
array([ 0.        ,  0.05093191,  0.10133397,  0.15068182,  0.19846207,
        0.24417767,  0.28735302,  0.32753895,  0.36431739,  0.39730573,
        0.42616078,  0.45058236,  0.47031639,  0.48515758,  0.49495154,
        0.49959636,  0.49904374,  0.49329942,  0.48242315,  0.4665281 ,
        0.44577962,  0.42039355,  0.39063401,  0.35681059,  0.31927516,
        0.27841822,  0.23466481,  0.18847011,  0.1403147 ,  0.09069956,
        0.04014084, -0.01083548, -0.06169907, -0.11192079, -0.16097816,
       -0.20836083, -0.25357585, -0.29615286, -0.3356489 , -0.37165308,
       -0.40379085, -0.43172786, -0.45517347, -0.47388378, -0.48766414,
       -0.49637119, -0.49991433, -0.49825672, -0.4914156 , -0.47946214])
In [17]:
def update_line(A=0.5):
    y = A*np.sin(t)
    line.set_ydata(y)
In [18]:
update_line(A=0.2)
In [19]:
widget = interact(update_line, A=FloatSlider(value=0.5, min=0.0, max=5.0))