Python:Plot scipy plot on top of voronoi diagram

偶尔善良 提交于 2021-01-28 11:58:52

问题


I am trying to plot on top of scipy plot. Used [this solution] (Python: plot on top of scipy plot? (voronoi)) but still cannot get single plot. Can anyone help? Thanks in advance... Sample plots are the following and trying to overlap them: Voronoi Diagram and Scatter Plot

vor = Voronoi( points )
voronoi_plot_2d(vor)

import matplotlib.pyplot as plt
import csv
x = []
y = []

with open('Junctions.csv','r') as csvfile:
    plots = csv.reader(csvfile, delimiter=',')
    for row in plots:
        x.append((row[0]))
        y.append((row[1]))

fig,ax = plt.subplots()
ax.scatter(x,y,s=5,color='r')

for i, txt in enumerate(n):
    ax.annotate(txt, (x[i], y[i]), size = 6)

plt.show()

回答1:


You can send the same axes to both the Voronoi plot and the scatter plot. The voronoi_plot_2d function includes the axes as an argument also:

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()

Using those four lines (and some arbitrary data) generated this plot:



来源:https://stackoverflow.com/questions/63182111/pythonplot-scipy-plot-on-top-of-voronoi-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!