[Tutor] read in ascii and plot
questions anon
questions.anon at gmail.com
Tue Dec 1 20:15:38 CET 2009
I would now like to add a line of best fit. I think the command is
polyfit()??
But I can't seem to get it to work
f=open('e:/testscatter.txt')
data=[map(float,line.split()) for line in f]
x, y=zip(*data)
pylab.polyfit(x,y,1)
pylab.scatter(x,y)
pylab.show()
Any feedback will be greatly appreciated.
On Mon, Nov 30, 2009 at 7:03 PM, Kent Johnson <kent37 at tds.net> wrote:
> On Mon, Nov 30, 2009 at 8:26 PM, Wayne Werner <waynejwerner at gmail.com>
> wrote:
>
> > A sample of the data is always helpful, but I'll take a shot in the dark.
> > If you have data like this:
> > 2.31 72
> > 98 23
> > ... ....
> > 34 7.32
> > And those are x y pairs you could do something like this:
> > f = open('input.txt')
> > #List comprehension to read all the lines as [[x1, y1], [x2, y2], ...
> [xn,
> > yn]]
> > data = [line.split() for line in f]
>
> You have to convert the text strings to float somewhere, for example
> data = [ map(float, line.split()) for line in f ]
>
> > # Reorient values as [(x1, x2,... xn), (y1, y2, ... yn)]
> > data = zip(*data)
> > # plot the xy vals
> > pylab.scatter(data[0], data[1])
>
> Or, IMO a little clearer,
> x, y = zip(*data)
> pylab.scatter(x, y)
>
> Kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091201/3491049d/attachment.htm>
More information about the Tutor
mailing list