2

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?

dreamin
  • 187
  • 1
  • 3
  • 12
  • What do you mean by *"I need the coordinates"*? You already have these - the `X` and `Y` arrays contain the coordinates associated with each value in `Z`. – ali_m Apr 17 '17 at 18:14
  • I am trying to create an isopleth on a leaflet map. I don't think I can just plot the X, Y coordinates because this is not the same as performing kde. Therefore, I was wondering if instead of creating a matplotlib graph (which shows an isopleth), I can do this in leaflet or I could get the positions after kde which is at the lines `kernel = stats.gaussian_kde(values)` `Z = np.reshape(kernel(positions).T, X.shape)` which I could then easily display on a leaflet map. Also, I have used the package leaflet.heat to obtain a heat map but I am not sure if this is the same things as kde. – dreamin Apr 17 '17 at 22:54
  • I'm still not sure what you mean by *"get the positions after kde which is at the lines `kernel = stats.gaussian_kde(values) Z = np.reshape(kernel(positions).T, X.shape)`"*. `Z` is not a set of lines, it's a rectangular matrix, where the value at `Z[i, j]` corresponds to the KDE at coordinate `X[i, j], Y[i, j]`. If you want contour lines then you could first create a contour plot using matplotlib, then extract the coordinates of the lines from this (e.g. http://stackoverflow.com/a/18309914/1461210). I've never used leaflet.js before, so I don't know whether it's capable of doing this natively. – ali_m Apr 18 '17 at 08:42

0 Answers0