2

I have a set of data of 2 columns and with this data I made a scatterplot and a curve fit of the form a*X^b, where my data 'H' is on the x-axis and 'Q' on the y-axis, but I also want to display the equation in the plot and its correlation coeficient R (just what Excel does) but I can't get it. I appreciate all the suggestions and comments.

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

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • 1
    try changing the `'ro'` in the plot into `'r-'`. It will connect the points with lines – Ha Bom Aug 20 '20 at 01:28
  • I find this answer to be very helpful[https://stackoverflow.com/questions/3938042/fitting-exponential-decay-with-no-initial-guessing)](https://stackoverflow.com/questions/3938042/fitting-exponential-decay-with-no-initial-guessing) – r-beginners Aug 20 '20 at 02:07
  • Thank you for your answers. I have a problem using 'r-', because when I do it it connects the point in a very strange way, so this is why I used 'ro' instead @HaBom – Jairo Vargas Aug 21 '20 at 01:50

0 Answers0