如何在Python中显示曲线拟合线的方程式?

2024-09-24 22:26:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组两列的数据,用这些数据我做了一个散点图和一个a*X^b形式的曲线拟合,其中我的数据“H”在X轴上,“Q”在y轴上,但我也想在图中显示方程及其相关系数R(就像Excel那样),但我无法得到它。我感谢所有的建议和评论

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
datos=pd.read_excel('AforosLaBalsa.xlsx')
datos=datos[datos.Nivel !=0] #Elimina las filas donde la columna Nivel tenga 0
H = datos['Nivel']
Q = datos['Caudal']


#Se define la función para el ajuste
def regrpot(x,a,b):
    return a*x**b
Qn=Q+0.2*np.random.normal(size=len(H))
ajuste,cov= curve_fit(regrpot,H,Qn)
#Gráficos
fig=plt.figure(figsize=(5,5))
plt.plot(H,Q,'b^', label='Datos')
plt.xlabel('Nivel (m)')
plt.ylabel(r'$Q (m^{3}/s)$')


plt.plot(H,regrpot(H,*ajuste),'ro',label='Curva ajustada:') #Gráfico de la curva de ajuste
plt.legend()
plt.show()

enter image description here


Tags: 数据importasnppltlafitpd