In [2]:
from ipywidgets import *
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
%matplotlib inline

In [24]:
phase = widgets.IntSlider()
display(phase)
i = 0

xdata = []
ydata = []

fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(xdata, ydata, 'b-') # Returns a tuple of line objects, thus the comma
plt.ylim(0,200)
plt.xlim(0,100)

def on_value_change(change):
    global i
    i += 1
    phaseval = change['new']
    a1 = 0.5
    a2 = 0.5
    a3 = (np.exp(1j*phaseval)*a1 + 1j*a2)/np.sqrt(2)
    a4 = (1j*np.exp(1j*phaseval)*a1 + a2)/np.sqrt(2)
    xdata.append(i)
    ydata.append(a4)
    line1.set_ydata(ydata)
    line1.set_xdata(xdata)
    print(xdata)
    
    

phase.observe(on_value_change, names='value')


[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

In [10]:
a1 = 0.5
a2 = 0.5
a3 = (np.exp(1j*phaseval)*a1 + 1j*a2)/np.sqrt(2)
a4 = (1j*np.exp(1j*phaseval)*a1 + a2)/np.sqrt(2)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-b5e0097c3e6f> in <module>()
      1 a1 = 0.5
      2 a2 = 0.5
----> 3 a3 = (np.exp(1j*phaseval)*a1 + 1j*a2)/np.sqrt(2)
      4 a4 = (1j*np.exp(1j*phaseval)*a1 + a2)/np.sqrt(2)

NameError: name 'phaseval' is not defined

In [ ]:


In [32]:
xdata = []
ydata = []
y2data = []


for i in range(50):
    phaseval = i*np.pi/10
    a1 = 0.5
    a2 = 0.5
    a3 = (np.exp(1j*phaseval)*a1 + 1j*a2)/np.sqrt(2)
    a4 = (1j*np.exp(1j*phaseval)*a1 + a2)/np.sqrt(2)
    xdata.append(phaseval)
    ydata.append(a3*a3.conjugate())
    y2data.append(a4*a4.conjugate())

fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(xdata, ydata, 'b-') # Returns a tuple of line objects, thus the comma
line2, = ax.plot(xdata, y2data, 'b-') # Returns a tuple of line objects, thus the comma


/Users/dawes/anaconda/lib/python3.5/site-packages/numpy/core/numeric.py:474: ComplexWarning: Casting complex values to real discards the imaginary part
  return array(a, dtype, copy=False, order=order)

In [ ]: