gpt4 book ai didi

python KDE 将轮廓和路径转换为特定的 json 格式 leaflet-friendly

转载 作者:太空狗 更新时间:2023-10-29 21:09:25 25 4
gpt4 key购买 nike

我正在用 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 库使用。

更新:

我的输入数据结构由三列组成,逗号分隔:

  1. 第一个是X值
  2. 第二个是Y值
  3. 第三个是我的数据的ID,它没有数值,只是数据点的标识符。

更新 2:

问题,如果简单地说,我想要使用 numpy 数组格式的输入文件获得与上述链接中相同的输出。

更新 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是一个将 matplotlib 轮廓转换为 geojson 的 python 库

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 KDE 将轮廓和路径转换为特定的 json 格式 leaflet-friendly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44152524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com