4

I have a multidimensional histogram

   H=histogramdd((x,y,z),bins=(nbins,nbins,nbins),range=((0,1),(0,1),(0,1)))

I need to print in an array the values of H which are different from zero and I also need to know the coordinate/the bins where this happens.

I am not familiar with tuples. Can you help me?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Brian
  • 13,996
  • 19
  • 70
  • 94
  • What are you using to create histograms? What/where is the definition of `histogramdd`? – Karl Knechtel Aug 02 '11 at 08:39
  • It is a standard numpy function (http://nullege.com/codes/search/numpy.histogramdd). What I have is a set of points whose coordinates are x,y,z and whose range in between 0 and 1. I am diving the "cube" in nbins and counting how many points I have in these bins. I just need to access these points. – Brian Aug 02 '11 at 08:43
  • The first parameter is supposed to be the sample data. A histogram stores several points; in your example line of code, you have described one point: `(x, y, z)`. You should instead have something like `((x1, y1, z1), (x2, y2, z2), ...)`. The result is an `ndarray`, so I don't really understand why you're asking about tuples. – Karl Knechtel Aug 02 '11 at 08:46
  • Sorry. I forgot to write that my x, y and z are array themselves. x=(x1,x2,x3...), the same for y and z. – Brian Aug 02 '11 at 08:50
  • Wait, wait, wait. You have an array of all the x coordinates for your points, and then an array of all the y coordinates, and then an array of all the z coordinates? And you want to match them up to make points? Is that the real question? – Karl Knechtel Aug 02 '11 at 08:51
  • Yes it is. I have a set of 3d points in a cube of size 1 and I just need to count how many of them are in the bins that I chose. That's it. Sorry if I was unclear. – Brian Aug 02 '11 at 09:02

1 Answers1

5

use where to find the index of nozeros in H, and use the index to get the coordinate:

import numpy as np
x = np.random.random(1000)
y = np.random.random(1000)
z = np.random.random(1000)
nbins = 10
H, [bx, by, bz]=np.histogramdd((x,y,z),bins=(nbins,nbins,nbins),range=((0,1),(0,1),(0,1)))

ix, iy, iz = np.where(H)

for t in zip(bx[ix], by[iy], bz[iz], H[ix,iy,iz]):
    print t
HYRY
  • 94,853
  • 25
  • 187
  • 187