I have simple x,y data from a csv file of which I want to plot a linear fit. I followed the example in the first answer to this question: Linear regression with matplotlib / numpy
My code looks like this:
#!/usr/bin/env python
import matplotlib.axes as ax
import matplotlib.pyplot as plt
import numpy as np
import csv
import seaborn
from scipy import stats
x = []
y = []
z = []
with open('Data.csv','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
x.append(float(row[0]))
y.append(float(row[2]))
xarray = np.array(x) #Convert data from csv into arrays
yarray = np.array(y)
m,b = np.polyfit(xarray,yarray,1)
plt.plot(xarray, yarray,'b+', m*xarray+b,'--k')
plt.plot(x,y,'ko')
f = [28.45294177, 61.06207611, 85.51892687,115.21653136,143.7495239] #this is the array
resulting from m*x+b
plt.plot(m*xarray+b)
plt.plot(x,f, 'r+')
plt.xlabel('Masse [kg]')
plt.ylabel('Auslenkung[mm]')
ax = plt.gca()
ax.set_xlim([0,0.3])
plt.title('')
plt.grid(True, linestyle = '--') #enable Grid, dashed linestyle
plt.show()
The output is:
However, the resulting Graph (Blue line) is not at all how it is to be expected, the slope is way to small. When I get the values of the array that results from the m*x+b function and plot it, the values correspond to the expected linear regression and to the actual Data (red pluses)
Honestly, I am at wits end here. I can't seem to figure out where my mistake is and neither do I understand where the blue line results from.
Any help would be greatly appreciated
Data.csv
file (perhaps copy paste it here, looks like it is only 5 points) – norok2