# -*- coding: utf-8 -*-
"""
Created on Wed Jan 16 22:58:47 2019

@author: pacar
"""

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from pylab import *

def f(x,T):
    return np.tanh(695*x/float(T))

B=0

#pour un reseau avec 6 voisins et une energie d'interacation de 0.1eV




x= np.linspace(-2,2,10000)
M = x-B



plt.figure(figsize=(20,15))
ax = plt.subplot(111)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
# ax.spines['left'].set_smart_bounds(True)
# ax.spines['bottom'].set_smart_bounds(True)
#plt.xlabel('$x=\\frac{\\mu_BB}{k_BT}$',fontsize=18)
#plt.ylabel('$\\frac{M}{\\frac{N}{V}\\mu_B}$',fontsize=20,rotation=0,labelpad=20)
#ax.set_title('$M=\\frac{N}{V}\\mu_Btanh(\\frac{\\mu_BB}{k_BT})$',fontsize=22)
ax.set_ylim(bottom=-1.2,top=1.2)
plt.xticks([-1,0,1])
plt.yticks([-1,0,1])


ax1, = plt.plot(x,M,label='$y=\\frac{M}{M_{sat}}-KB_0$')
l, = plt.plot(x,f(x,298),label='$y=tanh(\\frac{JZ}{k_BT}M)$',color='k')



plt.legend(fontsize=16,loc=2)




axcolor = 'lightgoldenrodyellow'
axalpha = axes([0.6, 0.1, 0.3, 0.03], facecolor=axcolor)
axb = plt.axes([0.6, 0.2, 0.3, 0.03], facecolor=axcolor)


salpha = Slider(axalpha, '$T(K)$', 1, 1500, valinit=298)
sb = Slider(axb, '$B_0$', -2, 2.0, valinit=B)

salpha.label.set_fontsize(20)
sb.label.set_fontsize(20)

def update(val):
    ax1.set_ydata(x-sb.val)
    l.set_ydata(f(x, salpha.val))

salpha.on_changed(update)
sb.on_changed(update)

plt.show()

















