import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
# Загрузка и предварительная обработка данных
increase = np.loadtxt("increase.txt", delimiter=" ", dtype=np.float)
decrease = np.loadtxt("decrease.txt", delimiter=" ", dtype=np.float)
dataIncrease = [[], []]
dataDecrease = [[], []]
for element in increase:
dataIncrease[0].append(1 / element[0])
dataIncrease[1].append(np.log(element[1] * 133.3))
for element in decrease:
dataDecrease[0].append(1 / element[0])
dataDecrease[1].append(np.log(element[1] * 133.3))
dataIncrease = np.array(dataIncrease)
dataDecrease = np.array(dataDecrease)
# Аппроксимация
approxDegree = 1 # Степень многочлена, которым аппроксимируем. Два замечательно подходит
fp, residuals, rank, sv, rcond = sp.polyfit(dataDecrease[0], dataDecrease[1], approxDegree, full=True)
f = sp.poly1d(fp)
print(f) # Выведет многочлен со всеми коэффициентами
# Вывод графика
fig, ax = plt.subplots()
ax.plot(dataIncrease[0], dataIncrease[1], marker='.', markersize=10, markerfacecolor='#e74c3c', color='#e74c3c', ls='None')
ax.plot(dataDecrease[0], dataDecrease[1], marker='.', markersize=10, markerfacecolor='#2980b9', color='#2980b9', ls='None')
ax.plot(dataDecrease[0], f(dataDecrease[0]), color='#000000', linewidth=2) # Рисуем аппроксимационную кривую
ax.set_xlabel('1 / T, $K^{-1}$')
ax.set_ylabel('ln(P), ln(Па)')
ax.text(300.5, 7000, 'Повышение', color='#e74c3c')
ax.text(298.5, 8000, 'Понижение', color='#2980b9')
ax.grid()
plt.show()