from scipy import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib as mpl
import sys
import matplotlib.patches as mpatches
from matplotlib.legend_handler import HandlerPathCollection

mpl.rcParams['pdf.fonttype'] = 42
mpl.rcParams['ps.useafm'] = True
mpl.rcParams['pdf.use14corefonts'] = True
#mpl.rcParams['usetex'] = True

def latexify(fig_width=None, fig_height=None, columns=1):
    """Set up matplotlib's RC params for LaTeX plotting.
    Call this before plotting a figure.

    Parameters
    ----------
    fig_width : float, optional, inches
    fig_height : float,  optional, inches
    columns : {1, 2}
    """

    # code adapted from http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples

    # Width and max height in inches for IEEE journals taken from
    # computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf

    assert(columns in [1,2])

    if fig_width is None:
        fig_width = 3.39 if columns==1 else 6.9 # width in inches

    if fig_height is None:
        golden_mean = (sqrt(5)-1.0)/2.0    # Aesthetic ratio
        fig_height = fig_width*golden_mean # height in inches

    MAX_HEIGHT_INCHES = 8.0
    if fig_height > MAX_HEIGHT_INCHES:
        print("WARNING: fig_height too large:" + fig_height + 
              "so will reduce to" + MAX_HEIGHT_INCHES + "inches.")
        fig_height = MAX_HEIGHT_INCHES

    params = {'backend': 'ps',
              'text.latex.preamble': ['\usepackage{gensymb}'],
              'axes.labelsize': 20, # fontsize for x and y labels (was 10)
              'axes.titlesize': 8,
              'text.fontsize': 14, # was 10
              'legend.fontsize': 12, # was 10
              'xtick.labelsize': 20,
              'ytick.labelsize': 20,
              'text.usetex': True,
              'figure.figsize': [fig_width,fig_height],
              'font.family': 'serif'
    }

    mpl.rcParams.update(params)

omega_scph = np.loadtxt('calc_frequency_T.dat')
omega_harm = 2.701221e+01 
omega_exp1 = np.loadtxt('BGG_Eg.txt')
omega_exp2 = np.loadtxt('BGG_T2g.txt')

figure = plt.figure(figsize=(6,8))
ax = plt.subplot(111)

latexify()

#ax.set_xscale('log')
#ax.tick_params(axis='both', which='major', labelsize=16)


plot1 = ax.plot(omega_scph[:,0], omega_scph[:,1], marker='None', color='red', linestyle='-', label=r"Anharmonic (SCPH)", lw=2)
plot2 = ax.axhline(y=omega_harm, color='black', linestyle='--', label=r"Harmonic", lw=2)
m1 = ax.plot(omega_exp1[:,0], omega_exp1[:,1], marker='o', linestyle='None', ms=10, mew=1.5, mfc = 'None', mec='blue', label=r"Expt. (Takasu et al.)", lw=2)
m2 = ax.plot(omega_exp2[:,0], omega_exp2[:,1], marker='*', linestyle='None', ms=10, mew=1.5, mfc = 'None', mec='blue', label=r"Expt. (Takasu et al.)", lw=2)

ax.axis([0, 500, 25, 40])
ax.axes.set_xlabel(r"Temperature (K)", fontsize=20)
ax.axes.set_ylabel(r"Frequency (cm$^{-1}$)", fontsize=20)

#ax.legend([AnyObject(), m1[0]], ['Expt. (Takasu et al.)'], handler_map={AnyObject: data_handler()})

ax.legend(loc='upper left', fancybox=False, numpoints=1, fontsize=16)
plt.savefig("rattling.pdf", format='pdf', dpi=300, rasterized=True, bbox_inches='tight')


plt.show()

