您可以将相同的轴发送到 Voronoi 图和散点图。这voronoi_plot_2d
函数还包含轴作为参数:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import voronoi_plot_2d, Voronoi, KDTree
x = [.22, .2, .4, .44, .42, .61, .17, .2, .63, .66]
y = [.21, .43, .23, .41, .42, .31, .2, .17, .62, .65]
points = np.array([[.1, .1], [.12, .44], [.11, .7], [.39, .09], [.41, .5], [.7, .14], [.71, .65]])
vor = Voronoi(points)
tree = KDTree(points)
locs, ids = tree.query(list(zip(x,y)))
fig,ax = plt.subplots(1,1)
voronoi_plot_2d(vor,ax)
ax.scatter(x,y,s=20,color='r')
for i in range(0,len(x)):
ax.annotate(ids[i], (x[i], y[i]), size = 10)
plt.show()
使用这四行(和一些任意数据)生成了这个图: