Line of best fit

J

Jamie Mitchell

I am new to python so apologies for the ignorance with this question.

How would I apply a line of best fit to a plot?

My data are netCDF4 data files and this is essentially what I have done so far:

swh1=netCDF4.Dataset('filename','r')
hs1=swh1.variables['hs']

swh2=netCDF4.Dataset('filename'.'r')
hs2=swh2.variables['hs']

plt.plot(hs1,hs2,'.')

Cheers,

Jamie
 
M

Moritz Beber

None of these are in the standard library but why re-invent the wheel?

Using numpy:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html

scipy:
http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.stats.linregress.html

statsmodels:
http://statsmodels.sourceforge.net/devel/examples/notebooks/generated/ols.html


I am new to python so apologies for the ignorance with this question.

How would I apply a line of best fit to a plot?

My data are netCDF4 data files and this is essentially what I have done so
far:

swh1=netCDF4.Dataset('filename','r')
hs1=swh1.variables['hs']

swh2=netCDF4.Dataset('filename'.'r')
hs2=swh2.variables['hs']

plt.plot(hs1,hs2,'.')

Cheers,

Jamie
 
R

Roy Smith

Jamie Mitchell said:
I am new to python so apologies for the ignorance with this question.

How would I apply a line of best fit to a plot?

Python has nothing built-in which does that, but there are plenty of
add-on modules which do those sorts of things. One popular one is
statsmodels (http://statsmodels.sourceforge.net/)
plt.plot(hs1,hs2,'.')

Please tell us more about the environment you're working in. I'm
guessing from the fact that you're calling plt.plot(), that you've
already got some add-on modules loaded. Pandas, maybe?
 
S

Sturla Molden

Roy Smith said:
Please tell us more about the environment you're working in. I'm
guessing from the fact that you're calling plt.plot(), that you've
already got some add-on modules loaded. Pandas, maybe?

Probably matplotlib.pyplot
 
S

Steven D'Aprano

I am new to python so apologies for the ignorance with this question.

How would I apply a line of best fit to a plot?

That depends on what software you are using to generate the plot. I see
you have this line of code:
plt.plot(hs1,hs2,'.')

but you haven't told us what plt is, where it comes from, or anything
needed for us to answer your question.

So, start by telling us what plt is, and we may be able to tell you
whether or not it supports lines of best fit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top