[Tutor] read in ascii and plot

Wayne Werner waynejwerner at gmail.com
Tue Dec 1 02:26:33 CET 2009

On Mon, Nov 30, 2009 at 5:55 PM, questions anon <questions.anon at gmail.com>wrote:

> I would like to read in two columns of data from a *.txt file
> I type
> f=open("e:/testascii.txt")
> import pylab
> pylab.scatter(f)
> and then receive an error.
> How do I point it to each column and do I need to do anything about the
> space gap between the two columns?
> Thanks in advance.

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,
data = [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])

That should be something along the lines of what you're looking for.

To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091130/9e241f80/attachment.htm>

More information about the Tutor mailing list