Python KDE gets paths and paths to a specific json booklet format

I do a kernel density estimate in Python and get the paths and paths as shown below. (here is my data: https://pastebin.com/193PUhQf ).

from numpy import * from math import * import numpy as np import matplotlib.pyplot as plt from scipy import stats x_2d = [] y_2d = [] data = {} data['nodes'] = [] # here is the sample data: # https://pastebin.com/193PUhQf X = [.....] for Picker in xrange(0, len(X)): x_2d.append(X[Picker][0]) y_2d.append(X[Picker][1]) # convert to arrays m1 = np.array([x_2d]) m2 = np.array([y_2d]) x_min = m1.min() - 30 x_max = m1.max() + 30 y_min = m2.min() - 30 y_max = m2.max() + 30 x, y = np.mgrid[x_min:x_max:200j, y_min:y_max:200j] positions = np.vstack([x.ravel(), y.ravel()]) values = np.vstack([m1, m2]) kde = stats.gaussian_kde(values) z = np.reshape(kde(positions).T, x.shape) fig = plt.figure(2, dpi=200) ax = fig.add_subplot(111) pc = ax.pcolor(x, y, z) cb = plt.colorbar(pc) cb.ax.set_ylabel('Probability density') c_s = plt.contour(x, y, z, 20, linewidths=1, colors='k') ax.plot(m1, m2, 'o', mfc='w', mec='k') ax.set_title("My Title", fontsize='medium') plt.savefig("kde.png", dpi=200) plt.show() 

There is a similar way to get outlines using R, which is described here: http://bl.ocks.org/diegovalle/5166482

Question: how can I achieve the same result using my python script or as a starting point? the desired result should look like contours_tj.json , which can be used by leaflet.js lib.

UPDATE:

My input structure consists of three columns separated by commas:

  • first is the value of X
  • the second is the value of Y
  • The third is the identifier of my data, it has no numerical value, it is simply the identifier of the data point.

Update 2:

The question, if you just put it, is that I want to get the same result as in the link above using my input file, which is in the numpy array format.

update 3:

my input structure is a list type:

 print type(X) <type 'list'> 

and here are the first few lines:

 print X[0:5] [[10.800584, 11.446064, 4478597], [10.576840,11.020229, 4644503], [11.434276,10.790881, 5570870], [11.156718,11.034633, 6500333], [11.054956,11.100243, 6513301]] 
+7
json python r kde leaflet
source share
2 answers

geojsoncontour is a python library for converting matplotlib outlines to geojson

geojsoncontour.contour_to_geojson requires the contour_levels argument. Levels in pyplot.contour are automatically selected, but you can access them using c_s._levels

So, for your example, you can do:

 import geojsoncontour # your code here c_s = plt.contour(x, y, z, 20, linewidths=1, colors='k') # Convert matplotlib contour to geojson geojsoncontour.contour_to_geojson( contour=c_s, geojson_filepath='out.geojson', contour_levels=c_s._levels, ndigits=3, unit='m' ) 
+1
source share

Now focus on this

There is a similar way to get outlines using R, which is described here: http://bl.ocks.org/diegovalle/5166482

I ask you to consider the rpy2 package in Python if this is doable for you

http://rpy.sourceforge.net/rpy2/doc-2.4/html/introduction.html

0
source share

All Articles