Skip to content Skip to sidebar Skip to footer

Python: Plot On Top Of Scipy Plot? (voronoi)

how do I plot on top of a voronoi plot (which is a scipy plot)? Note my question is slightly different than here where they explain how to color a voronoi plot For instance, imagin

Solution 1:

I think you can simply reuse plot like this:

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import voronoi_plot_2d, Voronoi

points = np.array([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]])
v = Voronoi(points)
voronoi_plot_2d(v)

p2 = [[0.25, 1], [1, 0.75], [1.75, 0.25], [1.75, 1.75]]
x, y = zip(*p2)

plt.scatter(x, y, color='r')
plt.show()

Voronoi + points

Solution 2:

I am having the same problem. Here is a function which can do it explicitly. Note: It is dropping the points at infinity. There is a lot of space for improvement there; feel free to edit.

defplot_vor_edges(vor, ax=None):

    if ax isNone:
        ax = plt.axes()

    ver = vor.vertices

    for reg in vor.regions:
        # Remove vertices at infinityiflen(reg)>=1:
            if -1in reg:
                reg = np.roll(reg, -reg.index(-1))
                reg = [x for x in reg if x != -1]

                regionX = ver[reg, 1]
                regionY = ver[reg, 0]
            else:
                regionX = ver[reg + [reg[0]], 1]
                regionY = ver[reg + [reg[0]], 0]

            ax.plot(regionX, regionY, '-k', linewidth=0.5)
        else:
            continue

Post a Comment for "Python: Plot On Top Of Scipy Plot? (voronoi)"