import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import os
import seaborn as sns
from lmfit import Model

g2x_0 = np.genfromtxt("g2x_0_fit.dat",skip_header=1).T
plt.clf()
plt.rcParams['font.size'] = '22'
fig, axs = plt.subplots(figsize=(11.69,4),constrained_layout=True)
# plot g2x(0)
axs.axvline(36.4,0,1,color="k",linestyle="--")
# axs.text(36.4,0.47,'lasing threshold',ha='right',va='bottom',rotation='vertical',fontsize=18,fontstyle="italic")
axs.errorbar(g2x_0[0],g2x_0[1],yerr=g2x_0[2],fmt='ro')
axs.set_ylabel(r"$g^{(2X)}(0)$")
# axs[2].yaxis.set_label_position("right")
# axs[2].yaxis.tick_right()
axs.set_ylim([0.45,1.05])
axs.set_yticks([0.5,0.75,1])
axs.set_xlabel("injection current [mA]")

# weird region
start_current = 46.4
axs.axvline(start_current,0,1,color="k",linestyle="dotted")

end_current = 48.4
axs.axvline(end_current,0,1,color="k",linestyle="dotted")


ax2=axs.twinx()
# ax2.errorbar(g2x_0[0],(1-g2x_0[1])/0.5*100,yerr=g2x_0[2],fmt='r.')
ax2.set_ylim([-10,110])
ax2.invert_yaxis()
ax2.set_ylabel("% coherent light",labelpad=10,rotation=90,va='center',ha='center')
# fig.tight_layout()
print(np.amax((1-g2x_0[1])/0.5))
plt.savefig("g2x_0_vs_current.png",format="png",dpi=1000)
# plt.show()