I've tried to resolve this code. I do not understand why the generated red lines are not located in their respective dot graphs. I have an exponential function evaluated for each one. Who can help me solve this code? I would like to generate a correct regression for each graph with its regression points and lines respectively. Thank you all.
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
#data
data_complete=[[92.5215,55.0136,40.5884,24.1340,14.3502],
[108.5836,64.5642,47.6347,28.3238,16.8414],
[123.9908,73.7254,54.3937,32.3427,19.2311],
[143.9338,85.5835,63.1425,37.5448,22.3242],
[158.8782,94.4696,69.6985,41.4430,24.6421]]
columns_values=[10,20,30,60,120]
rows_values=[5,10,20,50,100]
#generate matrix(pandas)
matrix_values=pd.DataFrame(data_complete,index=rows_values, columns=columns_values)
def func(x,a,b):
return a*(x**b)
for m in range(0,len(rows_values)):
plt.plot(columns_values,matrix_values.iloc[m],'bo', label='point')
plt.title("Graphic - " "Col = " str(rows_values[m]), fontdict={'fontweight':'bold', 'fontsize':10})
popt, pcov=curve_fit(func,columns_values,matrix_values.iloc[m])
print(popt)
xFit = np.arange(120.0,3.0, -8)
fig, axes=plt.subplots()
axes.plot(xFit, func(xFit,*popt),'r',label='Parameters: a=%5.3f, b=%5.3f' % tuple(popt)) #plotea solo los puntos gracias a ('o')
plt.xlabel('axis x')
plt.ylabel('axis y')
plt.legend()
plt.show()
CodePudding user response:
That's just a question of order. I guess this does as desired:
import pandas as pd
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
#data
data_complete=[[92.5215,55.0136,40.5884,24.1340,14.3502],
[108.5836,64.5642,47.6347,28.3238,16.8414],
[123.9908,73.7254,54.3937,32.3427,19.2311],
[143.9338,85.5835,63.1425,37.5448,22.3242],
[158.8782,94.4696,69.6985,41.4430,24.6421]]
columns_values=[10,20,30,60,120]
rows_values=[5,10,20,50,100]
#generate matrix(pandas)
matrix_values=pd.DataFrame(data_complete,index=rows_values, columns=columns_values)
def func(x,a,b):
return a*(x**b)
for m in range(0,len(rows_values)):
fig, axes=plt.subplots()
axes.plot(columns_values,matrix_values.iloc[m],'bo', label='point')
axes.set_title("Graphic - " "Col = " str(rows_values[m]), fontdict={'fontweight':'bold', 'fontsize':10})
popt, pcov=curve_fit(func,columns_values,matrix_values.iloc[m])
print(popt)
xFit = np.arange(120.0,3.0, -8)
axes.plot(xFit, func(xFit,*popt),'r',label='Parameters: a=%5.3f, b=%5.3f' % tuple(popt)) #plotea solo los puntos gracias a ('o')
axes.set_xlabel('axis x')
axes.set_ylabel('axis y')
axes.legend()
plt.show()
CodePudding user response:
I believe that there may be a simpler way to fit a line to a set of values. Numpy contains a few methods that do it easier and faster.
import numpy as np
x_data = np.array([11, 19, 31, 39, 51])
print(x_data)
y_data = np.array([5, 8, 32, 84, 110])
print(y_data)
import matplotlib.pyplot as plt
ylog_data = np.log(y_data)
print(ylog_data)
curve_fit = np.polyfit(x_data, log_y_data, 1)
print(curve_fit)
source:
https://www.geeksforgeeks.org/how-to-do-exponential-and-logarithmic-curve-fitting-in-python/