read a table and make a basic plot

Peter Otten __peter__ at
Mon Dec 19 04:01:46 EST 2016

metal.suomi at wrote:

> Hi, I'm learning python and full of extensive tutorials around. Getting a
> bit lost and overflowed in my head with tuples, dictionaries, lists, etc
> ... etc... Everything great, but I'd like to perform some basic task while
> learning the rest. For example, I'm having a hard time to find some
> practical info to get me up to speed. E.g. doing the following list of
> tasks:
> - read a file named 'data.dat' (let's suppose it has 13 columns and n
> lines) and read column 3 and 4
> -plot column 3 vs column 4
> -write a file 'outcome.txt' which has column 3 and 4
> How do I do read and write of files? Is there some extra library I need
> for doing plots? I have read about  matplotlib.pyplot. How can I do the
> plot above using this library? (running python3.5 in OSX).
> Thanks!
> J.

Here's a simple implementation that assumes both input and output file

$ cat
import csv
import sys

from matplotlib import pyplot

# get file names and column indices from commandline arguments
infile = sys.argv[1]
outfile = sys.argv[2]
xindex = int(sys.argv[3]) - 1
yindex = int(sys.argv[4]) - 1

# read the specified input file columns into separate lists
x = []
y = []
with open(infile) as instream:
    for row in csv.reader(instream, delimiter="\t"):

# write specified columns to output file
with open(outfile, "w") as outstream:
    csv.writer(outstream, delimiter="\t").writerows(zip(x, y))

# draw and show plot
pyplot.plot(x, y)

Use it like this:

$ python3 data.dat outfile.txt 3 4

PS: I used the following script to create some sample data:

import math
import random

ROWS = 100

def make_func(i):
    if i == 2:
        return lambda i: i
        N = random.randrange(1, 30)
        def f(i):
            return math.sin(i * N / ROWS)
        return f

column_funcs = [make_func(i) for i in range(13)]

with open("data.dat", "w") as outstream:
    for i in range(ROWS):
            "\t".join(map(str, [g(i) for g in column_funcs])),

More information about the Python-list mailing list