0

I am writing a code that allows me to obtain the linear regression of some measures. I have used different codes but with all of them I get strange result. Instead of being a line with a constant slope, the line I get is first horizontal and between the penultimate point and the last point the slope decreases. The code I am using is:

import matplotlib.pyplot as plt
import numpy as np

x0=[0.00000001,0.000001,0.0001,0.01]
y0=[0.9974209723854539,0.9945196648709005,0.9914759279447916,0.9852556749265332]


x=np.array(x0)
y=np.array(y0)

m,b=np.polyfit(x,y,1)
print(m,b)

plt.scatter(x,y)
plt.plot(x,m*x+b,color='green')

plt.xscale('log')
d=['linear fit   '+str(round(m,4))+'*x+'+str(round(b,4)),'real measure']

plt.legend(d,loc="upper right",borderaxespad=0.1,title="")

And I get the following graph: Phyton plot

Which is very different from what I should get, which I have been able to draw in Origin: Origin plot

I have tried various linear fit methods but with all of them I get this form which is wrong. Hopefully you can help me find the error. Thank you very much.

c993
  • 1
  • It looks like you have logarithmic data, as it appears linear only *after* you call `xscale('log')`. Have you considered this, or tried plotting it in linear space? – Felix Oct 23 '20 at 10:30
  • 1
    I agree with @Felix. np.polyfit is trying to fit logarithmic data to a first-degree polynomial...failure is expected. – tnknepp Oct 23 '20 at 10:32
  • It might be helpful to show your origin code as well. – tnknepp Oct 23 '20 at 10:32
  • Thank you so much for your replies. I'm working with real measure values. The x0 is the concentration used in a solution it is equivalent to (E-8,E-6,E-4 and E-2) and the y0 list is the measured amplitude and I am writting a Phyton code to treat this measurements. I have not worked a lot with Phyton plot obtions, so it is surely some basic error that I can not find. Thank you again for your help. – c993 Oct 23 '20 at 10:50
  • 1
    @c993 This is *probably* not an error in plotting, it's a mathematical error. You are trying to fit non-linear data to a linear equation. – tnknepp Oct 23 '20 at 11:08
  • To add to @tnknepp, the reason a linear plot would curve as we see in the example, is that if it contains three points instead of only the two ends, having anything else but linear axes makes it curve. – Felix Oct 23 '20 at 11:15
  • I think that you are right when I have plotted in Origin I have changed the scale in x axis to log10 before making the regression. How could I make that in Phyton? Thank you again – c993 Oct 23 '20 at 11:20
  • I'm pretty convinced now that Python or your plotting library has nothing to do with the difficulties you are facing. If this is a school assignment, I would suggest you learn something about nonlinear regression and work from there. A common trick (although sometimes inappropriate) is to transform the data to make it linear, then fit. Best of luck! – Felix Oct 23 '20 at 11:30
  • Ok, thank you a lot to both I have just solved it I only have to add to the code a little change and it works. Using m,b=np.polyfit(np.log10(x),y,1) works. As you have said it is my mathematical error. Thank you again you have help me a lot becouse I was obssesed thinking it was a problem with my Phyton linear regression method. – c993 Oct 23 '20 at 11:30

0 Answers0