python - How can I place a best fit line to the plotted points? -
i have simple plot containing 2 datasets in arrays
, , trying use regression calculate best fit line through points.
however line getting way off left , of data points.
how can line in right place, , there other tips , suggestions code?
from pylab import * = array([-13.74,-13.86,-13.32,-18.41,-23.83]) gra = array([31.98,29.41,28.12,34.28,40.09]) plot(gra,is,'kx') (m,b) = polyfit(is,gra,1) print(b) print(m) z = polyval([m,b],is) plot(is,z,'k--')
if curious, data bandgap of silicon transistor @ various temperatures.
you have careful of arrays pass x
coordinates , y
coordinates. consider have data values y
@ positions x
. have evaluate polynomial wrt. x
too.
from pylab import* = array([-13.74,-13.86,-13.32,-18.41,-23.83]) gra = array([31.98,29.41,28.12,34.28,40.09]) # rename variables clarity x = gra y = plot(x, y, 'kx') (m,b) = polyfit(x, y, 1) print(b) print(m) z = polyval([m,b], x) plot(x, z, 'k--') show()