代码之家  ›  专栏  ›  技术社区  ›  Andrew

是否有可能直接从之前的Delaunay三角剖分中绘制Voronoi细分?

  •  0
  • Andrew  · 技术社区  · 2 年前

    我有一个小的Python代码,绘制了一个小Delaunay三角剖分:

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

    你知道现在Python中是否可以直接在之前的图上叠加相应的Voronoi细分吗?

    我使用的是Python“3.9.7”和Matplotlib“3.8.4”

    1 回复  |  直到 2 年前
        1
  •  2
  •   Matt Pitkin    2 年前

    您可以使用 delauney_plot_2d voronoi_plot_2d 辅助函数,只需将Matplotlib轴对象传递给它们。例如。,

    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

    您可以在这些功能中自定义点/线样式等。

    推荐文章