In order to create a data density image I am follwing the calculations proposed in the Matlab code datadensity.m. It seemded a lot more straightforward than any [python codes][1] I found. However, it takes an incredible long amount of time to calculate the datapoints. Is there any way to speed things up? Is there a more efficient method using python syntax and/or speeding up the for-loops? My x and y data have many thousands of datapoints.
Here is my code:
# create random data
df_density = pd.DataFrame(np.random.randn(100000, 2), columns=list('xy'))
# width, height - dimensions of the density plot
width = 256
height = 256
# minimum and maximum of the input data
limits_1 = min(df_density.x)
limits_2 = max(df_density.x)
limits_3 = min(df_density.y)
limits_4 = max(df_density.y)
# resolution
deltax = (limits_2 - limits_1) / width
deltay = (limits_4 - limits_3) / height
# amount of smear, defaults to size of pixel diagonal
fudge = math.sqrt(deltax**2 + deltay**2)
dmap = np.zeros((height, width))
for ii in range(height-1):
yi = limits_3 + ii * deltay + deltay/2
for jj in range(width-1):
xi = limits_1 + jj * deltax + deltax/2
dd = 0
for kk in range(len(df_density)):
dist2 = (df_density.x[kk] - xi)**2 + (df_density.y[kk] - yi)**2
dd = dd + 1 / (dist2 + fudge)
dmap[ii,jj] = dd
[1]:e.g. Efficient method of calculating density of irregularly spaced points