import numpy as np
import pylab as py
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib import rc
from matplotlib import cm


def I(x, Q):
    return 1/(1+1j*Q*(x - 1/x))


def dessine(event):
    
    Q = amplitude1.val
    x = np.linspace(0.01,10,1000)
    y = np.abs(I(x,Q))
    phi = np.angle(I(x,Q)) 
    l1.set_xdata(x)
    l1.set_ydata(y)    
    l3.set_xdata(x)
    l3.set_ydata(phi)
    # l1.set_color(cm.jet(Q/10)[0:3])
    # l3.set_color(cm.jet(Q/10)[0:3])
    
    ax.axes.axis([0,5,0,1.1])
    ax.set_ylabel(r'${\frac{|\mathcal{I}|}{\mathcal{I}_0}}$', fontsize=16)
    
    
    bx.axes.axis([0,5,-3,3])
    bx.set_ylabel(r'$\varphi\,({\rm rad})$', fontsize=16)
    bx.set_xlabel(r'$\frac{\omega}{\omega_0}$', fontsize=16)

    
    py.draw()
    
    

fig = py.figure(figsize=[8,12])
ax = fig.add_axes([0.15,0.63,0.8,0.35])
l1,l2 = py.plot([],[],'-k' , [],[],'-k' )
ax.axes.axis([0,5,0,1.5])
ax.set_ylabel(r'${\frac{|\mathcal{I}|}{\mathcal{I}_0}}$', fontsize=16)


bx = fig.add_axes([0.15,0.21,0.8,0.35])
l3,l4 = py.plot([],[],'-k' , [],[],'-k' )
bx.axes.axis([0,5,-2,2])
bx.set_ylabel(r'${\varphi}$', fontsize=16)
bx.set_xlabel(r'$\frac{\omega}{\omega_0}$', fontsize=16)

    

sld_amplitude1 = py.axes([0.15, 0.05, 0.75, 0.05], facecolor='grey')
amplitude1 = Slider(sld_amplitude1, r'Q', 0.1, 10.0, valinit=5)
amplitude1.on_changed(dessine)


py.show()
