Brian
Brian

Reputation: 14836

Multidimension histogram in python

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?

Upvotes: 4

Views: 6116

Answers (1)

HYRY
HYRY

Reputation: 97271

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

Upvotes: 5

Related Questions