# [Numpy-discussion] Line of best fit!

James james@fmnmedia.co...
Tue Dec 9 05:13:24 CST 2008

```Hi,

Thanks for all your help so far!

Right i think it would be easier to just show you the chart i have so far;

--------------------------
import numpy as np
import matplotlib.pyplot as plt

plt.plot([4,8,12,16,20,24], [0.008,0.016,0.021,0.038,0.062,0.116], 'bo')

plt.xlabel("F (Number of washers)")
plt.ylabel("v^2/r ms-2")
plt.title("Circular Motion")
plt.axis([2,26,0,0.120])

plt.show()

------------------------

Very basic i know, all i wish to do is add a line of best fit based on
that data, in the examples there seems to be far more variables, do i
need to split my data up etc?

Thanks

Scott Sinclair wrote:
>> 2008/12/9 Angus McMorland <amcmorl@gmail.com>:
>> Hi James,
>>
>> 2008/12/8 James <james@fmnmedia.co.uk>:
>>
>>> I have a very simple plot, and the lines join point to point, however i
>>> would like to add a line of best fit now onto the chart, i am really new
>>> to python etc, and didnt really understand those links!
>>>
>>> Can anyone help me :)
>>>
>> It sounds like the second link, about linear regression, is a good
>> place to start, and I've made a very simple example based on that:
>>
>> -----------------------------------------------
>> import numpy as np
>> import matplotlib.pyplot as plt
>>
>> x = np.linspace(0, 10, 11) #1
>> data_y = np.random.normal(size=x.shape, loc=x, scale=2.5) #2
>> plt.plot(x, data_y, 'bo') #3
>>
>> coefs = np.lib.polyfit(x, data_y, 1) #4
>> fit_y = np.lib.polyval(coefs, x) #5
>> plt.plot(x, fit_y, 'b--') #6
>> ------------------------------------------------
>>
>
> James, you'll want to add an extra line to the above code snippet so
> that Matplotlib displays the plot:
>
> plt.show()
>
> Cheers,
> Scott
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>

```