# Coloring Voronoi cells in Delaunay Triangulations by area

**URL:** <https://discourse.julialang.org/t/coloring-voronoi-cells-in-delaunay-triangulations-by-area/105545>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [October 29, 2023, 5:41pm UTC](https://discourse.julialang.org/t/coloring-voronoi-cells-in-delaunay-triangulations-by-area/105545 "2023-10-29T17:41:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [October 29, 2023, 5:41pm UTC](https://discourse.julialang.org/t/coloring-voronoi-cells-in-delaunay-triangulations-by-area/105545/1 "2023-10-29T17:41:16Z")

</div>

I have a set of points which I have used the DelaunayTriangulation Package to generate voronoi cells. I have calculated the are of each cell, and now I want to color each cell according to the area. When I read the voronoiplot() documentation I know I can pass a color argument using `color=`, and I know DelaunayTriangulation uses CairoMakie. Can I just manually make a list of colors normalized by each cells area and pass that in? I don’t think I am constructing my color argument correctly!

I know I can also use the voronoicells package, but I want to clip my tessellations to the convex hull so DelaunayTriangulation is the best package for me!

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [October 29, 2023, 9:05pm UTC](https://discourse.julialang.org/t/coloring-voronoi-cells-in-delaunay-triangulations-by-area/105545/2 "2023-10-29T21:05:54Z")

</div>

Suppose that the voronoi diagram has n polygons and you have computed their area (here as a vector). If you saved areas in a different container, then you should adapt the following lines of code  
to your setup.  
To compare visually the area of polygons, it is recommended to use a colorscheme, not an arbitrary collection of colors.

```julia
area =2.5 .+3.2*rand(15)
amax, amin=extrema(area)
cs=ColorSchemes.haline
polygon_colors = [get(cs, (a-amin)/(amax-amin)) for a in area]

```

I expect that `voronoiplot` can use these colors for your polygons.  
I haven’t installed Makie to ckeck how `voronoiplot` works.

---

<div class="post-metadata">

**Author:** ![charperflow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charperflow/32/22252_2.png) [@charperflow](https://discourse.julialang.org/u/charperflow)\
**Post date:** [October 29, 2023, 10:19pm UTC](https://discourse.julialang.org/t/coloring-voronoi-cells-in-delaunay-triangulations-by-area/105545/3 "2023-10-29T22:19:03Z")

</div>

Ok this is what I was looking for! I calculated the areas and then performed an inverse normal transform on them so that the values could easily be used for a colorscale…Ill try this out and let you know how it goes

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [October 30, 2023, 6:17am UTC](https://discourse.julialang.org/t/coloring-voronoi-cells-in-delaunay-triangulations-by-area/105545/4 "2023-10-30T06:17:45Z")

</div>

I believe you can also pass `:extrema` to `get` to simplify a bit, e.g.

```julia
using DelaunayTriangulation, CairoMakie, ColorSchemes
tri = triangulate_rectangle(0, 1, 0, 1, 10, 10)
vorn = voronoi(tri, true)
areas = get_area.(Ref(vorn), 1:num_polygons(vorn))
colors = get(cgrad(:haline), areas, :extrema)
voronoiplot(vorn, color=colors)

```

![e262617807aa351bcab36c4e029d8645](https://global.discourse-cdn.com/julialang/original/3X/1/d/1d867a345b2ed4db6d8046e790bc0da8c8775810.png)
