2

Obviously, there are loads of threads concerning index errors. But I couldn't find one that helped me out.

I use numpy.loadtxt to read in a function f(a,b).

a, b, f = np.loadtxt(filename, delimiter=' ', usecols=(0,1,2), unpack=True)

To get a 2d plot I found a solution in another thread, where you raster the (a,b)-plane and associate an integer tuple (ida, idb) with each point.

ncols, nrows = np.round((a.max() - a.min()) / da).astype(np.int) , np.round((b.max() - b.min()) / db).astype(np.int)
ida = np.round((a - a.min()) / da - 1).astype(np.int)
idb = np.round((b - b.min()) / db - 1).astype(np.int)

Then put f into the a grid.

grid = np.empty((nrows, ncols), dtype=np.float)
grid[idb, ida] = f

This worked out well for several data files. But now, suddenly it doesn't. Instead it reports the error in the title when reaching the grid[ida,idb]-line. So I looked for a mistake in the data file, but I couldn't find any. I tried

print ida.shape
print idb.shape
print f.shape

which returns

(3107,)
(3107,)
(3107,)

so all arrays match well in size and especially none of them is an empty array. What am I missing here?

Thanks, Alice

4
  • The interesting object is grid... Commented Dec 7, 2013 at 12:40
  • can you post complete error traceback? Commented Dec 7, 2013 at 13:36
  • Thanks to seberg's advice, I found the mistake: In loadtxt I imported the wrong columns from the file. One happened to be constant. So I got nrows=0. And grid was the 0-size-array. Commented Dec 7, 2013 at 13:54
  • 1
    I can't close the thread thoguh without an answer it seems. And I can't write an answer myself until 8h have passed. Commented Dec 7, 2013 at 13:55

1 Answer 1

1

Thanks to seberg's advice, I found the mistake (and I also found old stackoverflow account):

a, b, f = np.loadtxt(filename, delimiter=' ', usecols=(0,1,2), unpack=True)

I imported the wrong columns from the file. One happened to be constant. So I got nrows=0. And grid was the 0-size-array.

Sorry...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.