Colorize Voronoi Diagram

后端 未结 3 935
悲哀的现实
悲哀的现实 2020-11-27 10:45

I\'m trying to colorize a Voronoi Diagram created using scipy.spatial.Voronoi. Here\'s my code:

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


        
3条回答
  •  执笔经年
    2020-11-27 11:11

    I have a much simpler solution to this problem, that is to add 4 distant dummy points to your point list before calling the Voronoi algorithm.

    Based on your codes, I added two lines.

    import numpy as np
    import matplotlib.pyplot as plt
    from scipy.spatial import Voronoi, voronoi_plot_2d
    
    # make up data points
    points = np.random.rand(15,2)
    
    # add 4 distant dummy points
    points = np.append(points, [[999,999], [-999,999], [999,-999], [-999,-999]], axis = 0)
    
    # compute Voronoi tesselation
    vor = Voronoi(points)
    
    # plot
    voronoi_plot_2d(vor)
    
    # colorize
    for region in vor.regions:
        if not -1 in region:
            polygon = [vor.vertices[i] for i in region]
            plt.fill(*zip(*polygon))
    
    # fix the range of axes
    plt.xlim([0,1]), plt.ylim([0,1])
    
    plt.show()
    

    Then the resulting figure just looks like the following.

提交回复
热议问题