joe-jordan / pyvoro

2D and 3D Voronoi tessellations: a python entry point for the voro++ library
Other
100 stars 26 forks source link

Plotting the result of the voronoi partition #15

Open AntixK opened 7 years ago

AntixK commented 7 years ago

Hi, I wanna know how to plot the result of the Voronoi partition in Matplotlib and uniquely colourize the individual faces.

Can you show me a example?

RussianJohn commented 6 years ago

Hi, Due to your question is still open and based on https://stackoverflow.com/questions/20515554/colorize-voronoi-diagram/20678647#20678647 example could be like that:

# python 3.5, pyvoro 1.3.2, linux x64
import matplotlib
import matplotlib.pyplot as plt
import pyvoro
import numpy as np

dots_num = 50
colors = np.random.rand(dots_num, 3) # or predefined 
dots = np.random.rand(dots_num, 2)
# make color map (unnecessary for just random colorization)
color_map = {tuple(coords):color for coords, color in zip(dots, colors)}

cells = pyvoro.compute_2d_voronoi(
dots, # point positions, 2D vectors this time.
[[0.0, 1.0], [0.0, 1.0]], # box size
2.0 # block size
)

# colorize
for i, cell in enumerate(cells):    
    polygon = cell['vertices']
    plt.fill(*zip(*polygon), color = color_map[tuple(cell['original'])], alpha=0.5)

plt.plot(dots[:,0], dots[:,1], 'ko')
plt.xlim(-0.1, 1.1)
plt.ylim(-0.1, 1.1)

plt.show()