[Tutor] read in ascii and plot

Kent Johnson kent37 at tds.net
Tue Dec 1 04:03:06 CET 2009


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


More information about the Tutor mailing list