2

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()
  • You need to leave out `fig,ax = plt.subplots()`, because that creates a complete new plot. Instead, use `ax = plt.gca()`. Also, `plt.scatter(x,y,s=2)` followed by `ax.scatter(x,y,s=5,color='r')` looks quite strange. – JohanC Jul 30 '20 at 22:36
  • Thanks for your feedback but still get two plots and you are right just corrected the two scatter functions... – Extrafantastik Jul 30 '20 at 22:53
  • You might want to edit your question to show the updated code that doesn't work. Otherwise, it is hard to guess what's going wrong. – JohanC Jul 31 '20 at 05:51

1 Answers1

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:

resulting plot

Alex
  • 1,042
  • 1
  • 8
  • 17
  • Thanks for your answer but i receive "MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0. was_held = getattr(ax, 'ishold', lambda: True)()" and still not have overlapped plots. The only one I get is voronoi. Can you share the whole code? – Extrafantastik Jul 31 '20 at 13:13
  • I updated this with the full code I used to generate the plot (and a new image to match the plot produced for this actual data). – Alex Jul 31 '20 at 13:55
  • Thanks @Alex it worked :) The last question: do you have idea on annotate of scatter? Since there are many points, I want to detect which one belongs to which cell. – Extrafantastik Jul 31 '20 at 14:52
  • You want to find which Voronoi cell each point belongs to (to annotate the points)? Do you need a search tree like in the example code [here](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.KDTree.query.html) to find the nearest voronoi site (i.e., which Voronoi cell each point belongs to)? – Alex Jul 31 '20 at 15:45
  • Exactly! Do you think this technique works for finding the corresponding cell for each point? – Extrafantastik Jul 31 '20 at 22:41
  • Yes, the nearest neighbor search is exactly finding which Voronoi cell each point belongs to. – Alex Aug 01 '20 at 10:16
  • Many thanks. I have some more questions now. The first one is how can I determine which cells are connected to each other i.e. cell connectivity matrix? And how to determine border points? According to your previous answer two points whose distances are the same to centers but as a counter example, out of these regions may also have equidistant points to centers. How to distinguish them? – Extrafantastik Aug 04 '20 at 09:30
  • Re: cell connectivity. The scipy Voronoi diagram doesn't have an easy way to get this information. But the Delaunay triangulation (which is the dual of the Voronoi diagram) does. Edges in the Delaunay diagram correspond to pairs of Voronoi cells that touch each other. The [scipy Delaunay triangulation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.Delaunay.html) result contains the vertex_neighbor_vertices data structure that holds in the info about which Delaunay vertices are connected, i.e., which Voronoi cells are adjacent. – Alex Aug 04 '20 at 10:18
  • Dealing with equidistant vertices can be challenging. Most Delaunay/Voronoi software attempts to use exact arithmetic meaning points that are close to equidistant (even very near floating point precision) get associated to a single cell. For cases that are truly equidistant, there is usually a convention followed (the earliest vertex is selected). These libraries probably won't help you with special handling of points on or near the Voronoi boundaries. – Alex Aug 04 '20 at 10:22