import numpy as np
import matplotlib.pyplot as plt

R = 8.314

def get_data(filename):
    with open(filename,'r') as file:
        data = np.array([line.split() for line in file.readlines()])
    return np.transpose(data)


data_matrix = get_data('data_oving_5.txt')
p_points = np.array([float(x) for x in data_matrix[1][1:]])
T_points = np.array([float(x) for x in data_matrix[0][1:]])

y_points = np.log(p_points)
x_points = -(1/(R*T_points))

fit_data = np.polyfit(x_points,y_points,1,cov=True)

a,b = fit_data[0]
s = np.sqrt(fit_data[1])
s_a,s_b = s[0][0], s[1][1]

x_line = np.linspace(min(x_points),max(x_points),100)

linreg = (a*x_line) + b

upper = ((a-s_a)*x_line) + b+s_b
lower = ((a+s_a)*x_line) + b-s_b

print('∆_vapH = ', -a, '±', 2*s_a)
print('c = ', b, '±', 2*s_b)

plt.plot(x_line,linreg)
plt.scatter(x_points,y_points)
plt.plot(x_line,lower, color='r')
plt.fill_between(x_line,upper,lower, alpha=0.3)
plt.show()