Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting the result of the voronoi partition #15

Open
AntixK opened this issue May 11, 2017 · 1 comment
Open

Plotting the result of the voronoi partition #15

AntixK opened this issue May 11, 2017 · 1 comment

Comments

@AntixK
Copy link

AntixK commented May 11, 2017

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
Copy link

RussianJohn commented Jul 29, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants