python - 顶点数组上的 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 合作,所以如果有人能看一看,那就太好了。
当前结果:
解决方案
AttributeError: 'NoneType' object has no attribute 'close'
我刚刚在使用 dstack 和 Voronoi 时遇到了问题。
np.dstack((x,y))
返回 [[[x1, y1],[x2, y2],....]] 请注意您需要将行更改为的额外括号
all_coords = np.dstack((x,y))[0]
我不确定另一个错误。我不明白。
推荐阅读
- c# - 使用 x 和 y 坐标定位 2d 单元格
- naudio - 如何在不使用 wav 文件的情况下在 NAudio 中组合多个信号以进行同步播放?
- php - 使用自定义 Wordpress 查询调用 5 个最近的帖子
- python - 使用词嵌入的句子相似度
- javascript - 如何阻止 ASP.NET Core 2.0 MVC 缩小修改特定文件?
- ios - 屏幕上任意位置的默认 interactivePopGestureRecognizer
- go - Goroutines 矩阵乘法错误
- java - mockito 中的模拟方法返回空指针异常?
- unit-testing - 在没有杂乱的类型转换的情况下使用 jest.mock 和 TypeScript?
- c - C 编程 - 从 char * 转换为 char 数组?