首页 > 解决方案 > 顶点数组上的 Voronoi 图

问题描述

我正在尝试创建示例地图的图表。我将地图的 x 和 y 顶点输入到一个数组中,然后执行

import matplotlib.pyplot as plt
import numpy as np
import math
import pandas as pd
import seaborn as sns
from scipy import *
from scipy.spatial import Voronoi, voronoi_plot_2d

#reads shapefile of region
cities = gpd.read_file('belgian_cities.shp')

#plots the cities
cities.plot()

cities.plot(cmap = 'jet')

g = [i for i in cities.geometry]
x,y = g[0].exterior.coords.xy
all_coords = np.dstack((x,y)) ####

for interior in g[0].interiors: # for first feature/row
    x, y = interior.coords.xy
    coords = np.dstack((x,y))
    all_coords = np.append(all_coords, coords, axis=0)

vor = Voronoi(all_coords)
fig = voronoi_plot_2d(vor)
plt.show()

但是,当我运行代码时,我收到两个错误:

AttributeError:“NoneType”对象没有“关闭”属性

ValueError:缓冲区的维数错误(预期为 2,得到 3)

这是我第一次与 GeoPandas 和 Voronoi 合作,所以如果有人能看一看,那就太好了。

当前结果:

在此处输入图像描述

标签: pythongeopandasvoronoi

解决方案


AttributeError: 'NoneType' object has no attribute 'close'我刚刚在使用 dstack 和 Voronoi 时遇到了问题。

np.dstack((x,y))返回 [[[x1, y1],[x2, y2],....]] 请注意您需要将行更改为的额外括号 all_coords = np.dstack((x,y))[0]

我不确定另一个错误。我不明白。


推荐阅读