I have an image made of many dots of 5 different colours.
I'd like to create a Voronoï diagram where every created region around a dot is filled with the same colour.
I'm able to create the diagram without colours, but when I want to add them they don't match their original position. I also noticed that some colours appears more than they should.
Here is my code snippet :
infos = list(zip(dataFrame["centroid y"], dataFrame["centroid x"],dataFrame["classe"]))
points =[([i,j],k ) for i,j,k in infos]
points_dic = {tuple(coord) : value for coord, value in points}
couleur = {0 : [1.,0.,0.],1:[1.,1.,0.],2:[0.,1.,0.],3:[0.53,0.81,0.92],4:[1.,1.,1.]}
centre = []
for coords, _ in points :
centre.append (coords)
vor = Voronoi(centre)
fig, ax = plt.subplots()
voronoi_plot_2d(vor, show_vertices=False, point_size=1, line_width=0.1, ax=ax)
for region, point_idx, points_coord in zip(vor.regions, vor.point_region,vor.points):
valeur = points_dic[points_coord[0],points_coord[1]]
couleur_region = couleur[valeur]
vertices = [vor.vertices[i] for i in region]
ax.fill(*zip(*vertices), color=couleur_region)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Voronoi Diagram with Colored Regions')
plt.show()
centroid x, y
correspond to the initial positions of my dots and classe
is an int that gives the matching colour in the couleur dictionary.
I obtain those results which as you can see are wrong :
The first image is my Voronoi diagram obtained thanks to the image on the right.
I noticed that my y axis is in the opposite direction but I checked the coordinates of my dataframe and the one on the voronoi diagram are identical.
First I verified that my center are well placed on my voronoi diagram by creating an image with only the centroids of my initials regions and they do. The plotted image
Then I checked manually that colors associated to every region (couleur_region)
correspond to their colour in my initial image and saw no mistakes.
I don't know what to do next.
It seems that the error is in the plotting with the ax.fill
but I can't see it.
Or maybe the index_point of the region are not matching the right region.