your mail

Cameron Simpson cs at
Fri Oct 17 12:43:16 CEST 2014

On 17Oct2014 11:45, Dhananjay <dhananjay.c.joshi at> wrote:
>This might be simple, but I guess I am missing something here.
>I have data file as follows:
>2.1576318858 -1.8651195165 4.2333428278
>2.1681875208 -1.9229968780 4.1989176884
>2.3387636157 -2.0376253255 2.4460899122
>2.1696565965 -2.6186941271 4.4172007912
>2.0848862071 -2.1708981985 3.3404520962
>2.0824347942 -1.9142798955 3.3629290206
>2.0281685821 -1.8103363482 2.5446721669
>2.3309993378 -1.8721153619 2.7006893016
>2.0957461483 -1.5379071451 4.5228264441
>2.2761376261 -2.5935979811 3.9231744717
>(total of 200 lines)
>Columns 1,2,3 corresponds to x,y,z axis data points.
>This is not a continuous data. I wish to make a plot as a 2D with 3rd
>dimension (i.e z-axis data) as a color map with color bar on right hand side.
>As a beginner, I tried to follow tutorial with some modification as follows:

Initially I've just got comments about the code in general, though they may 
help you find your issues.

># Read data from file:
>fl1 = open('flooding-psiphi.dat','r').readlines()

This reads all the lines into memory, into the list "fl1".
For 200 lines this is not a bit issue, but since you process them immediately 
you are usually better off reading the file progressively.

>xs = ys = zs = []

This is actually a bug in your code. I would guess you've based it on other 
example code such as:

   x = y = z = 0

The problem is that both these assignment statements assign the _same_ object 
to all 3 variables. With an int or a str this isn't an issue because they are 
immutable; any further math or modification actually makes new objects.

However, you use:

   xs = ys = zs = []

This makes exactly _one_ list, and assigns it ("binds it" in Python terms) to 
all three names.

The result is that when you append to the list, you're appending to the same 
list every time. Effectively this means (1) that xs, ys and zs have the same 
values and (2) they're 3 times as long as they should be.

Do this:

   xs = []
   ys = []
   zs = []

That makes three separate lists.

>for line in fl1:
>    line = line.split()
>    xs.append(float(line[0]))
>    ys.append(float(line[1]))
>    zs.append(float(line[2]))

Returning to the "reading the file progressively" thing, for a really big file 
(and as a matter of general practice) you're better off writing this like:

   for line in open('flooding-psiphi.dat','r'):
       line = line.split()

which only ever reads one line of text from the file at a time.

Start by fixing the:

   xs = ys = zs = []

assignment and see how the behaviour changes.

Cameron Simpson <cs at>

More information about the Python-list mailing list