Hi, I am pretty new to python (and programming in general), I am trying to create a curve fit to my data,
I am loading two arrays 'Tnn_month' and 'amon_month' from .mat files, I want to create a scatter plot of these two parameters and then assign a curve fit to show the correlation between both,
I tried to apply the curve_fit examples done on simple plots, but I still get this error: error: Result from function call is not a proper array of floats.
Tnn_month (x) and amon_month (y) are both arrays of length 5433656
find attached the scatter plot
-----------------------------------------------------
input:
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as io
import pandas as pd
import seaborn as sns
from matplotlib.pyplot import figure
MainFolder= r"/home/abeed/Documents/thesis/python/scatter/"
yyyymm_str= '201005'
d_or_n = 'day'
colors = (0,0,0)
area = np.pi*3
#load data
mat = io.loadmat('Tnn_amon0.01_201005.mat')
Tnn_month = mat['Tnn_index']
amon_month = mat['amon_index']
colors=np.array([0.1, 0.5, 0.5])
# scatter plot
figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
plt.scatter(Tnn_month, amon_month, s=area, c=colors, alpha=0.5)
plt.title('May 2010', fontsize=14)
plt.ylabel('Ammonia Total Concentration (molec/cm$^{2}$)',fontsize=14)
plt.xlabel('Skin Temperature (Kelvin)', fontsize=14)
plt.savefig(MainFolder + yyyymm_str + '_test_' + d_or_n +'.png', format='png', dpi=700)
#plt.show()
# curve_fit
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
Tnn_month[np.isnan(Tnn_month)]=0 #something for nans
amon_month[np.isnan(amon_month)]=0
def func(X, a, b, c):
return a * np.exp(-b * X) + c
Y = func(Tnn_month, 2.5, 1.3, 0.5)
np.random.seed(len(Tnn_month))
print(Tnn_month)
popt, pcov = curve_fit(func, Tnn_month, amon_month)
plt.plot(Tnn_month, func(Tnn_month, *popt), 'g--',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.show()
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
output:
[[272.64697509]
[273.43998094]
[272.89872854]
...
[265.03563904]
[264.87200613]
[273.74241245]]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
ValueError: object too deep for desired array
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-2-2ed29e6832ed> in <module>
71 print(Tnn_month)
72
---> 73 popt, pcov = curve_fit(func, Tnn_month, amon_month)
74
75 plt.plot(Tnn_month, func(Tnn_month, *popt), 'g--',label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
~/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, check_finite, bounds, method, jac, **kwargs)
750 # Remove full_output from kwargs, otherwise we're passing it in twice.
751 return_full = kwargs.pop('full_output', False)
--> 752 res = leastsq(func, p0, Dfun=jac, full_output=1, **kwargs)
753 popt, pcov, infodict, errmsg, ier = res
754 cost = np.sum(infodict['fvec'] ** 2)
~/anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
394 maxfev = 200*(n + 1)
395 retval = _minpack._lmdif(func, x0, args, full_output, ftol, xtol,
--> 396 gtol, maxfev, epsfcn, factor, diag)
397 else:
398 if col_deriv:
error: Result from function call is not a proper array of floats.