[英]Voronoi diagram in Manhattan metric
我正在使用scipy.spatial
來可視化 Voronoi 圖。 但是,這里使用的距離度量是歐幾里得 (L2)。 我正在我的 Voronoi 圖上尋找一種曼哈頓 (L1) 度量的方式。 有沒有一種簡單(或多或少)的方法來做到這一點?
import numpy as np
import matplotlib.pyplot as plt
points = np.array([[1.5, 1.], [3.5, 1.], [5., 2.], [2.5, 3.], [3.5, 1.], [4., 4.]])
from scipy.spatial import Voronoi, voronoi_plot_2d
vor = Voronoi(points)
fig = plt.figure()
ax = fig.add_subplot('111')
ax.plot(points[:, 0], points[:, 1], 'o', color='k')
ax.set_xlim([-1, 9])
ax.set_ylim([-1, 9])
voronoi_plot_2d(vor, ax)
基本上我想得到類似的東西,但在 L1 指標中。
我發現scipy.spatial.distance.cityblock
可以處理感興趣的指標,但不完全確定如何實現它以使其工作?
如果區域的可視化和計算是您唯一的要求,您可以使用我們不久前做過的名為mcvoronoi的 pip 庫。 這是基於蒙特卡羅抽樣。 我添加了一個選項來更改此答案的距離度量。 更新版本(帶有距離度量選項)尚未在 pip 上發布,但您可以使用 github 主分支。 用法如下圖:
python example.py
example.py 由以下基本行組成:
lat_lon_area, mean_percentage_error = voronoi_area(points,voronoi_plot_enabled=True, NUM_COLORS=5, metric='manhattan')
圖像保存如下所示:
您當然可以通過增加采樣點的數量來使它們變得超級清晰。 還會生成顯示面積計算錯誤的錯誤 plot。
您可能希望使用更多 colors,但如果您有大量區域,則稍微多於4 個 colors可能就足夠了。
I created a github repo containing a Python package called voronoiz
that includes the functions voronoi_l1
(for computing the polygons of an L1 Voronoi diagram) and voronoi_grid
(for computing an image of the Voronoi diagram for any distance metric supported by scipy.spatial.cdist
) .
這些實現使用蠻力,O(n²) 算法,因此如果您將它用於數百萬個點,它可能無法正常工作,但對於少量到中等數量的點,您可以使用它來制作漂亮的圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.