"""
===========
Slider Demo
===========

Using the slider widget to control visual properties of your plot.

In this example, a slider is used to choose the frequency of a sine
wave. You can control many continuously-varying properties of your plot in
this way.
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
import math



B=0.01 #Valeur de B
T=300 ## Valeur T

#subplots
fig = plt.figure()
ax = fig.add_subplot(111)


plt.yticks(np.arange(2), ('0', '$M_{sat}$'))
plt.xlabel('$B (T)$')
plt.ylabel('$M (A.m^{-1})$', rotation= 'horizontal', linespacing=10.0)


plt.subplots_adjust(bottom=0.25)
x = np.arange(0.0, 4.0, 0.01)


f0 = 10

s = np.tanh(x/T*300)




p1, = ax.plot(x, s, lw=2)
p2, = ax.plot(x, np.ones(np.shape(x)[0]), 'g--')
p3, = ax.plot(x, x/T*300, 'r--')
p4, = ax.plot(B, math.tanh(B), 'ro')
plt.legend(['$M$', '$M_{sat}$', '$\\frac{\mu_B B}{k_BT}$'])

ax.axis([0, 4, -0, 1.5])


axcolor = 'lightgoldenrodyellow'
axb = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axT = plt.axes([0.25, 0.05, 0.65, 0.03], facecolor=axcolor)


sb = Slider(axb, '$B (T)$', 0, 4.0, valinit=B)
sT = Slider(axT, '$T (K)$', 1, 600, valinit=T)



def update(val):
    B = sb.val
    T = sT.val
    p1.set_ydata(np.tanh(x/T*300))
    p3.set_ydata(x/T*300)
    p4.set_xdata(B)
    p4.set_ydata(math.tanh(B/T*300))




sb.on_changed(update)
sT.on_changed(update)


resetax = plt.axes([0.8, 0.01, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')


def reset(event):
    sb.reset()

button.on_clicked(reset)



def colorfunc(label):
    l.set_color(label)
    fig.canvas.draw_idle()

plt.show()


