f



Line of best fit

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
0
Jamie
3/31/2014 11:29:15 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

4 Replies
579 Views

Similar Articles

[PageSpeed] 27

--001a113a6020e6901104f5e5a9f1
Content-Type: text/plain; charset=UTF-8

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


On Mon, Mar 31, 2014 at 1:29 PM, Jamie Mitchell <jamiemitchell1604@gmail.com
> wrote:

> 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
> --
> https://mail.python.org/mailman/listinfo/python-list
>

--001a113a6020e6901104f5e5a9f1
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">None of these are in the standard library but why re-inven=
t the wheel?<br><br>Using numpy:<br><a href=3D"http://docs.scipy.org/doc/nu=
mpy/reference/generated/numpy.polyfit.html">http://docs.scipy.org/doc/numpy=
/reference/generated/numpy.polyfit.html</a><br>
<br>scipy:<br><a href=3D"http://docs.scipy.org/doc/scipy-0.13.0/reference/g=
enerated/scipy.stats.linregress.html">http://docs.scipy.org/doc/scipy-0.13.=
0/reference/generated/scipy.stats.linregress.html</a><br><br>statsmodels:<b=
r>
<a href=3D"http://statsmodels.sourceforge.net/devel/examples/notebooks/gene=
rated/ols.html">http://statsmodels.sourceforge.net/devel/examples/notebooks=
/generated/ols.html</a><br></div><div class=3D"gmail_extra"><br><br><div cl=
ass=3D"gmail_quote">
On Mon, Mar 31, 2014 at 1:29 PM, Jamie Mitchell <span dir=3D"ltr">&lt;<a hr=
ef=3D"mailto:jamiemitchell1604@gmail.com" target=3D"_blank">jamiemitchell16=
04@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I am new to python so apologies for the ignorance with this question.<br>
<br>
How would I apply a line of best fit to a plot?<br>
<br>
My data are netCDF4 data files and this is essentially what I have done so =
far:<br>
<br>
swh1=3DnetCDF4.Dataset(&#39;filename&#39;,&#39;r&#39;)<br>
hs1=3Dswh1.variables[&#39;hs&#39;]<br>
<br>
swh2=3DnetCDF4.Dataset(&#39;filename&#39;.&#39;r&#39;)<br>
hs2=3Dswh2.variables[&#39;hs&#39;]<br>
<br>
plt.plot(hs1,hs2,&#39;.&#39;)<br>
<br>
Cheers,<br>
<br>
Jamie<br>
<span class=3D"HOEnZb"><font color=3D"#888888">--<br>
<a href=3D"https://mail.python.org/mailman/listinfo/python-list" target=3D"=
_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div>

--001a113a6020e6901104f5e5a9f1--
0
Moritz
3/31/2014 11:50:53 AM
In article <0fb15100-15e8-46d6-a38f-b187c7012e62@googlegroups.com>,
 Jamie Mitchell <jamiemitchell1604@gmail.com> wrote:

> 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?
0
Roy
3/31/2014 12:21:48 PM
Roy Smith <roy@panix.com> wrote:

> 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

0
Sturla
4/1/2014 12:44:33 AM
On Mon, 31 Mar 2014 04:29:15 -0700, Jamie Mitchell wrote:

> 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.




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
0
Steven
4/1/2014 1:00:52 AM
Reply:

Similar Artilces:

Best fit line for an exponential line
Hi, I am new to using GNUPLOT and am trying to find the best fit line (to determine the slope) of a line that follows Power Law (is an exponential line). I realize I have to use a function f(x) = a*b**x. But, I do not know what to do next. Any help in this matter would be greatly appreciated. Thanks, Divya Divya Rao <divyarao09@yahoo.com> wrote: > Hi, > I am new to using GNUPLOT and am trying to find the best fit line (to > determine the slope) of a line that follows Power Law (is an > exponential line). First, a terminology issue: an exponential curv...

Best fit line?
Hi. Is there a function to calculate/plot the best fit line given a few (approx 3) points? I don't have any toolboxes, nor can I afford any. Can any function in the basic matlab application do this? Thanks! "Amir " <amirschricker.DELETE@DELETE.gmail.DELETE.com> wrote in message <g4o8va$aa7$1@fred.mathworks.com>... > Hi. Is there a function to calculate/plot the best fit line > given a few (approx 3) points? I don't have any toolboxes, > nor can I afford any. > > Can any function in the basic matlab application do this? Yes. help polyfit J...

Best Fit Line
Hello, I have a problem that is puzzling me for very long.I got this problem of fitting in 80 over text files and within each txt file,it contains about 120 datas.I want to from these datas generate a best fit line to cut through all these points.The program I am now using is Matlab.Please help and reply asap,as I need the info urgently. Thanks. In article <a488ubsdofj7@legacy>, kellyblue19@yahoo.com (Eid) writes: >Hello, > I have a problem that is puzzling me for very long.I got this >problem of fitting in 80 over text files and within each t...

best .lang. python
Any ideas on what to do? Stop attempting to negotiate. Contact your state insurance commission. Be prepared to divulge the name of your insurer. Keep detailed records of all your contacts. ----- - gpsman website--> http://www.win2job.info/ ...

line of best fit #2
I have two vectors and plotted them using a stem graph. I want to find the line of best fit and display this on the graph. I know about the function polyfit, but how do I graph its output? Look at help polyval cheers Peter wrote: > > > I have two vectors and plotted them using a stem graph. I want to > find the line of best fit and display this on the graph. I know > about > the function polyfit, but how do I graph its output? ...

Help With Best Fit Lines
Hello All: I am trying to compare an observed dataset with best-fit models for: 1) Power Law 2) Exponential 3) Exponential Truncated Power Law I am able to get these four datasets (observed data + 3 models). But now I wish to see which of the models fits the dataset the best. It seems MATLAB AIC will only fit your data to built-in models, and not these 3 models I approximated myself. Any suggestions for procedures (whether or not they use MATLAB or AIC) will be very helpful!! Thank you. Jo PS: The datasets are not the same size (i.e. observed x=50 points, y=50 points. models x=1030 poi...

Weighted Line of Best Fit
At the moment I am writing code to find the weighted line of best fit. I can calculate the line of best fit using the code given below, where sortWLAN corresponds to the entries in an array, which I want to extract. onedist = sortWLAN(3,1:100); onerssi = sortWLAN(4,1:100); onewgt = sortWLAN(5,1:100); coeffs = polyfit(onedist,onerssi, coefficient); curvefit = polyval(coeffs, distance); hold on plot(onedist, onerssi, 'o'); plot(distance,curvefit, 'r'); hold off Anybode have any ideas? Cheers Brad wrote: > At the moment I am writing code to find the weighted line of best &...

best fit line through coordinates
Hi guys, I have an issue I'd like some help with. coordinates = 107.35 107 111.51 146 117 154.61 111.29 114 In the above matrix 'coordinates', its actually in the form [ x1 y1 x2 y2] for each of the 2 rows. Is there any function I can obtain a best fit line using these 4 points? thanks Kurtis Go look up the demo on the function polyfit, u can tell it to make a first order regression of those...

Outputting lines of best fit
I have a set of data in an array (11000,207). I've been asked to detrend the data in each of the 207 columns individually. I've tried using the Matlab detrend but I cant get it to work in a sensible way for this data. Instead I've been trying to ouput the equation for a line of best fit for the data in each column and then subtract that from the data. Seems a simple task but the only way I have found to find a line of best fit is doing it manually once you have a figure up using the best fitting option in tools. Obviously I don't want to do this manually 207 times. Does any...

Line of best fit #3
I have a matrix simular to the one below: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 Is there a command that will give me an equation for a line of best fit for these data points? Thanks On Sep 27, 4:16=A0pm, "Charles " <charles.vaug...@colorado.edu> wrote: > I have a matrix simular to the one below: > > =A0 =A0 =A01 =A0 =A0 2 > =A0 =A0 =A02 =A0 =A0 3 > =A0 =A0 =A03 =A0 =A0 4 > =A0 =A0 =A04 =A0 =A0 5 > =A0 =A0 =A05 =A0 =A0 6 > =A0 =A0 =A06 =A0 =A0 7 > =A0 =A0 =A07 =A0 =A0 8 > > Is there a comm...

Best fit line in subplot
I want to add a linear trendline in both the plots of subplot. How do i do this, when i do this using the basic fitting option from the tools menu of figure, the line appears only for one data set ata time, i want to do it for both the data sets. You're right, it doesn't seem possible to do it with the Basic Fitting tool. I guess you have to do it by hand, like this: %%%%% x=[0:0.005:1.5]; %curve 1 y=sin(x); %linear fit 1 p = polyfit(x,y,1) y_lin = polyval(p,x); %curve 2 z=cos(x); %linear fit 2 p = polyfit(x,z,1) z_lin = polyval(p,x); %plot subplot(1,2,1) h(1)=plot(x,y) hold on h(2)=...

best fit line of an ROI
Hi everyone., The following code reads an image, allows user to select an ROI and then the intention is to select 1000 points randomly in the ROI and generate a best fit line through these points. By inserting breakpoints i see that the points generated randomly lie in the range of points belonging to the ROI. So, it seems there is no problem at this stage.I am not able to identify where else there is an error. Pls help. Thanx in advance. clc; close all; clear all; %WHEN USING THE IMAGE MAKE SURE THAT BW AND BW1 DO NOY HAVE TOO MANY ELEMENTS, IF THEY DO CROP BW AND BWI AND USE THE CROPPED...

nonlinear best-fit line
Good afternoon, I am having trouble trying to figure out how to fit a nonlinear best fit line to my data in SAS. Any help would be greatly appreciated. Regards, Ligia ...

Web resources about - Line of best fit - comp.lang.python

Weather Line 1.1
Not one but two weather app recommendations today. And this one, Weather Line, has become my primary weather app. I find the temperate graph ...

Power Line
Every year for some time now, has selected an English Premier League team of the year. Last year, in my absence, a long-time reader made the ...

Kaluzhsko-Rizhskaya Line - Wikipedia, the free encyclopedia
is a line of the Moscow Metro , that originally existed as two separate radial lines, Rizhskaya and Kaluzhskaya opened in 1958 and 1962, respectively. ...

Usman Khawaja in line for Indian Premier League pay day after Big Bash League heroics
Rod Marsh might have overlooked him but Australia's man of the moment Usman Khawaja could be hot property at this year's Indian Premier League ...

Disney movie princesses don’t get as many lines as male characters do
DISNEY princesses are being significantly outspoken by the men in their films.

Letters: Bottom line always at the top for neoliberalism
&nbsp;Readers are concerned about the social impacts of inaction on economic reforms.

Commuter chaos fears as buses replace trains on Frankston line for level crossing removal
Train travellers are fearing more commuter chaos this week after being left stranded on the Frankston line due to a lack of replacement buses ...

Abantus Saga 2 is a space-based, line-matching puzzler
... Abantus Saga 2 , offers exciting levels that can be completed in just a few minutes, if youre good enough. Organize the squares to form lines ...

AT&T Rising: Q4 Rev Misses, EPS In-Line
AT&T (T) this afternoon reported Q4 revenue that missed analysts’ expectations, and profit that was in line with consensus. Revenue in the three ...

Read between the lines, and the Cavs trading Kevin Love is on the table
There have been no serious trade talks reported, but there are indications in the wake of the firing of David Blatt that Cleveland could look ...

Resources last updated: 1/27/2016 2:37:46 AM