I have used the following code after following this tutorial to perform kde on coordinates (latitudes and longitudes) that represent the positions of trees from 2015 NYC street tree census.
import numpy as np
from scipy import stats
xmin, xmax = min(zip_latitudes), max(zip_latitudes)
ymin, ymax = min(zip_longitudes), max(zip_longitudes)
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([zip_latitudes, zip_longitudes])
kernel = stats.gaussian_kde(values)
Z = np.reshape(kernel(positions).T, X.shape)
I then plot the results which resemble a heatmap using matplotlib by doing:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,extent=[xmin, xmax, ymin, ymax])
ax.plot(zip_latitudes, zip_longitudes, 'k.', markersize=2)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
plt.show()
However, I need the coordinates in order to add them to a leaflet.js and mapbox.js plot. I tried printing some elements in Z
but I am not sure this is right. My plan was to use python to get the coordinates then write the results to a csv the use d3 and lealet.js to create the heatmaps on a map. Any suggestions?