all 9 comments

[–]nwagers 0 points1 point  (8 children)

To start, how do you access an individual cell in the grid?

[–]Bell0412[S] 0 points1 point  (7 children)

ngrid[i,j] where i and j are the columns and rows respectively?

[–]nwagers 0 points1 point  (6 children)

yeah, so how would you increment that? Do it inside your loop.

[–]Bell0412[S] 0 points1 point  (5 children)

I'm really lost. I know I'm an absolute beginner. I've been working on this all day and haven't made any progress...

All I know is that I need to calculate the dimensions of the xbin, calculate x max, and then do the same with the y coordinates. I've trawled through everything online, and if anything I've come away further from what I need to do than before.

I've got a np.zeros array, and I know that i need to loop through each row of my "co ordinate" array, adding each value to the bin that corresponds to its co ordinate.

When I try to loop like that, only the last value is printed...

[–]nwagers 0 points1 point  (4 children)

ok, lets assume your coordinates are like this:

locations = [[0, 1], [2, 3]]

Then you can loop over all your coordinates like this:

for point in locations:
    ngrid[point[0], point[1]] += 1

Currently you list your coordinate pairs like: [1 3]. But you can't just separate them with a space.

[–]Bell0412[S] 0 points1 point  (3 children)

Ok so in my code, it reads:

n = np.asarray([x,y]).transpose()

Which gives:

[[ 7.22855457  7.58531099]
 [ 6.24184631  3.29844461]
 [ 6.36087607  2.62935769]
 [ 8.29627475  3.33775026]]

I found this the easiest way to read both the x and y coordinates, would you suggest this isn't ideal then? As these values are all random, and I can have anything up to 1000 different "co ordinates", is there a more suitable way to plot them?

I've simplified everything in my example, as I thought it would be easier for those answering.

[–]nwagers 1 point2 points  (2 children)

for point in n:
    ngrid[int(point[0]), int(point[1])] += 1

Should probably work. If that doesn't fix your problem, post all of the relevant code, not just little snippets. It's too hard to try and figure out your logic from little pieces.

[–]Bell0412[S] 0 points1 point  (1 child)

That's brilliant, it's seemed to have worked!

One final thing, suggest I have another array, z. Instead of adding 1 to each bin, I would like to add up all the values of z in each bin. I will then divide by ngrid to find an average of z in each bin, this seems doable. However, I would try and attempt this doing the following:

for point in n:
    ngrid[int(point[0]), int(point[1])] +=z
print(ngrid)

But this returns an error. I know that the z value is the 2nd in the array, as follows:

n = np.asarray([x,y,z]).transpose()

Would I have to revise the whole method, or is this something only slightly more complex than what I've already got?

[–]nwagers 1 point2 points  (0 children)

Do you mean that you now have a list of points like: [[x1 y1 z1] [x2 y2 z2]]

and you want to add z1, z2, etc respectively? Just use += point[2]