0

I would like to use binned_statistic_2d (from scipy) on my dataset, but with custom bin edges calculated so that in every bin there is an equal number of points.

I tried to modify code from user farenorth for 2d dataset, but I'm beginner in python, and have trouble with it. That code (1d) is:

def histedges_equalN(x, nbin):
    npt = len(x)
    return np.interp(np.linspace(0, npt, nbin + 1),
                     np.arange(npt),
                     np.sort(x))
x = np.random.randn(100)
n, bins, patches = plt.hist(x, histedges_equalN(x, 10))

I just need it modified for 2d histogram. Can you show me how it's done?

elizevin
  • 101
  • 1
  • 9
  • Your "histogram" would be a list with length nbin, where every element is len(x) // nbin. A graph of this would be a flat line, and I wouldn't call it a histogram at all. Is that really what you want? Are you by any chance writing a "histogram equalization" function for image processing? – Paul Cornelius Mar 21 '18 at 19:57
  • I have 3d dataset (lets say x, y and z), and I want to use binned_statistic _2d to find median of z data while I'm binning x and y data. But I have very uneven number of datapoints in each bin if I use fixed number of equal length bins (well squares accually), I end up with more that 80% of my data in first couple of bins, which produces just couple of medians from almost all data. I tought the answer to that problem would be custom length bins with fixed number od datapoints in every bin. That is what I'm trying to do. I don't want to plot histogram, I need custom bin edges for x and y axis. – elizevin Mar 22 '18 at 09:13

0 Answers0