python
复制代码
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.font_manager import FontProperties
import sympy as sp
font = FontProperties(fname=r"C:\Windows\Fonts\SimHei.ttf", size=12)
excel_file_path = 'E:/python/air_values.xlsx'
# 读取 Excel 文件
df = pd.read_excel(excel_file_path)
x_T = df.iloc[:,0]
y_k = df.iloc[:,1]
y_m = df.iloc[:,2]
y_Cp = df.iloc[:,3]
fig, axs = plt.subplots(3, 1, figsize=(8, 12))
# 进行四次多项式拟合
coefficients_k = np.polyfit(x_T, y_k, 4)
poly_k = np.poly1d(coefficients_k)
x = sp.symbols('x')
poly_latex_k = sp.latex(sp.Poly(poly_k(x), domain='RR').as_expr().evalf(2))
x_fit = np.linspace(min(x_T), max(x_T), 100)
y_fit_k = poly_k(x_fit)
axs[0].set_title(r'k:$' + poly_latex_k + r'$', fontproperties=font)
axs[0].scatter(x_T, y_k, label='原始数据')
axs[0].plot(x_fit, y_fit_k, label='四次多项式拟合', color='red')
axs[0].legend(prop=font)
# 进行四次多项式拟合
coefficients_m = np.polyfit(x_T, y_m, 4)
poly_m = np.poly1d(coefficients_m)
poly_latex_m = sp.latex(sp.Poly(poly_m(x), domain='RR').as_expr().evalf(2))
y_fit_m = poly_m(x_fit)
axs[1].set_title(r'm:$' + poly_latex_m + r'$', fontproperties=font)
axs[1].scatter(x_T, y_m, label='原始数据')
axs[1].plot(x_fit, y_fit_m, label='四次多项式拟合', color='red')
axs[1].legend(prop=font)
# 进行四次多项式拟合
coefficients_Cp = np.polyfit(x_T, y_Cp, 4)
poly_Cp = np.poly1d(coefficients_Cp)
x = sp.symbols('x')
poly_latex_Cp = sp.latex(sp.Poly(poly_Cp(x), domain='RR').as_expr().evalf(2))
y_fit_Cp = poly_Cp(x_fit)
axs[2].set_title(r'Cp:$' + poly_latex_Cp + r'$', fontproperties=font)
axs[2].scatter(x_T, y_Cp, label='原始数据')
axs[2].plot(x_fit, y_fit_Cp, label='四次多项式拟合', color='red')
axs[2].legend(prop=font)
plt.tight_layout()
plt.show()