[Tutor] read in ascii and plot
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
But I can't seem to get it to work
data=[map(float,line.split()) for line in f]
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>
> > 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], ...
> > 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, data)
> Or, IMO a little clearer,
> x, y = zip(*data)
> pylab.scatter(x, y)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor