With numpy / scipy it looks like this:
import numpy as np import scipy.stats as stats x = np.random.uniform(0,200,100) y = np.random.uniform(0,200,100) w = np.random.uniform(1,10,100) h = np.histogram2d(x,y,bins=[10,10], weights=w,range=[[0,200],[0,200]]) hist, bins_x, bins_y = h q = stats.mstats.mquantiles(hist,prob=[0.25, 0.5, 0.75]) >>> q.round(2) array([ 512.8 , 555.41, 592.73]) q1 = np.where(hist<q[0],1,0) q2 = np.where(np.logical_and(q[0]<=hist,hist<q[1]),2,0) q3 = np.where(np.logical_and(q[1]<=hist,hist<=q[2]),3,0) q4 = np.where(q[2]<hist,4,0) >>>q1 + q2 + q3 + q4 array([[4, 3, 4, 3, 1, 1, 4, 3, 1, 2], [1, 1, 4, 4, 2, 3, 1, 3, 3, 3], [2, 3, 3, 2, 2, 2, 3, 2, 4, 2], [2, 2, 3, 3, 3, 1, 2, 2, 1, 4], [1, 3, 1, 4, 2, 1, 3, 1, 1, 3], [4, 2, 2, 1, 2, 1, 3, 2, 1, 1], [4, 1, 1, 3, 1, 3, 4, 3, 2, 1], [4, 3, 1, 4, 4, 4, 1, 1, 2, 4], [2, 4, 4, 4, 3, 4, 2, 2, 2, 4], [2, 2, 4, 4, 3, 3, 1, 3, 4, 4]])
prob = [0.25, 0.5, 0.75] - the default value for the quantile parameters, you can change it or leave it.