I'm trying to fit and plot a simple data in a .txt file with a linear function (a*x+b)
using matplotlib and scipy. I run into an error concerning the test function: "can't multiply sequence by non-int of type 'numpy.float64'"
I have tried changing the variable name x
but I get the same problem. Most of the code comes from a working one that is able top fit data without problems and uses the same definition for the test function.
import matplotlib.pyplot as plt
from scipy import optimize
import numpy as np
f=open("testData.txt","r")
x_data=[]
y_data=[]
trash=f.readline() #discards first line
for line in f: #reads x and y data from file
x_read,y_read=line.split()
x_data.append(float(x_read))
y_data.append(float(y_read))
def test_func(x, a, b):
return a*x+b
params, params_covariance = optimize.curve_fit(test_func, x_data, y_data,
p0=[1, 1])
plt.figure(figsize=(6, 4))
plt.scatter(x_data, y_data)
plt.plot(x_data, test_func(x_data, params[0], params[1]), label='Fitted
function')
plt.show()
This is the error :
Traceback (most recent call last):
File "C:/Users/Fra/Desktop/lab/ottica/2/reaqd.py", line 19, in plt.plot(x_data, test_func(x_data, params[0], params[1]), label='Fitted function')
File "C:/Users/Fra/Desktop/lab/ottica/2/reaqd.py", line 14, in test_func return a*x+b
TypeError: can't multiply sequence by non-int of type 'numpy.float64'
You're probably trying to multiply a Python list by a float, which does not work. Try a*np.array(x)+b