pythonpython-3.xmatplotlibvoronoidelaunay

Is it possible to plot a Voronoi tessellation directly from a previous Delaunay triangulation?


I have a small Python code plotting a small Delaunay triangulation:

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.spatial import Delaunay 

points = np.array([[0, 0], 
                   [1, 0], 
                   [0.5, 0.5],
                   [1 , 0.5]])
tri = Delaunay(points) 
fig, ax = plt.subplots(figsize=(8, 4))
ax.set_aspect('equal', 'box')
plt.triplot(points[:,0], points[:,1], tri.simplices.copy()) 
plt.plot(points[:,0], points[:,1], 'o') 
plt.show() 

Do you know if it's now possible in Python to directly superimpose on the previous plot the corresponding Voronoi tessellation?

I am using Python '3.9.7' and a Matplotlib '3.8.4'


Solution

  • You can use the delauney_plot_2d and voronoi_plot_2d helper functions and just pass the Matplotlib axes object to them both. E.g.,

    import numpy as np 
    import matplotlib.pyplot as plt 
    from scipy.spatial import Delaunay, Voronoi, voronoi_plot_2d, delaunay_plot_2d
    
    points = np.array([[0, 0], 
                       [1, 0], 
                       [0.5, 0.5],
                       [1 , 0.5]])
    tri = Delaunay(points) 
    fig, ax = plt.subplots(figsize=(8, 4))
    
    vor = Voronoi(points)
    
    _ = delaunay_plot_2d(tri, ax=ax)
    _ = voronoi_plot_2d(vor, ax=ax)
    
    ax.set_aspect('equal', 'box')
    

    enter image description here

    You can customise the points/line styles/etc in those functions.