python KDE 将轮廓和路径转换为特定的 json 格式传单友好
python KDE get contours and paths into specific json format leaflet-friendly
我正在 Python 中进行核密度估计并获得如下所示的轮廓和路径。 (这是我的示例数据: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()
有一种类似的方法可以使用 R 获取等高线,这里有描述:
http://bl.ocks.org/diegovalle/5166482
问题:如何使用 python 脚本或作为起点获得相同的输出?
所需的输出应该像 contours_tj.json 可以被 leaflet.js
lib.
使用
更新:
我的输入数据结构由三列组成,逗号分隔:
- 第一个是 X 值
- 第二个是Y值
- 第三个是我的数据的ID,没有数值,只是数据点的标识符。
更新 2:
问题,如果简单地说,我想要使用 numpy 数组格式的输入文件得到与上面 link 相同的输出。
更新 3:
我的输入数据结构是列表类型:
print type(X)
<type 'list'>
这是前几行:
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]]
geojsoncontour 是一个 python 库,用于将 matplotlib 等高线转换为 geojson
geojsoncontour.contour_to_geojson
需要一个 contour_levels
参数。 pyplot.contour
中的级别是自动选择的,但您可以使用 c_s._levels
访问它们
因此,对于您的示例,您可以这样做:
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'
)
我正在 Python 中进行核密度估计并获得如下所示的轮廓和路径。 (这是我的示例数据: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()
有一种类似的方法可以使用 R 获取等高线,这里有描述: http://bl.ocks.org/diegovalle/5166482
问题:如何使用 python 脚本或作为起点获得相同的输出?
所需的输出应该像 contours_tj.json 可以被 leaflet.js
lib.
更新:
我的输入数据结构由三列组成,逗号分隔:
- 第一个是 X 值
- 第二个是Y值
- 第三个是我的数据的ID,没有数值,只是数据点的标识符。
更新 2:
问题,如果简单地说,我想要使用 numpy 数组格式的输入文件得到与上面 link 相同的输出。
更新 3:
我的输入数据结构是列表类型:
print type(X)
<type 'list'>
这是前几行:
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]]
geojsoncontour 是一个 python 库,用于将 matplotlib 等高线转换为 geojson
geojsoncontour.contour_to_geojson
需要一个 contour_levels
参数。 pyplot.contour
中的级别是自动选择的,但您可以使用 c_s._levels
因此,对于您的示例,您可以这样做:
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'
)