According to mrz's question: Color code Voronoi cell areas depending on number of vertices, he colored voronoi cell by number of corners corresponding to a Voronoi cell.

My question is how to colorize voronoi cell by area of every cell, for example we have a mesh as follows:

pts = RandomReal[{-1, 1}, {25, 2}];

I want to colorize the cells with different gradient in gray,i.e., 0.1mm^2(light gray)~1mm^2(dark gray). Or add a color function to differentiate every cell based on their own area, in other words, the cells have same value in area share same color.

Thank you!


2 Answers 2

pts = RandomReal[{-1, 1}, {25, 2}];
v = VoronoiMesh[pts];
mp = MeshPrimitives[v, 2];
a = Rescale[Area /@ mp];
  MapThread[{ColorData["Rainbow"][#1], #2} &, {a, mp}]], 

The barlegend is scale 0 to 1 relative to maximum area. Obviously this can be modified to achieve desired result.

enter image description here

  • $\begingroup$ Thank you ubpdqn! Your answer works well in my case. Regards,Shuoqi $\endgroup$
    – Shuoqi Li
    Apr 4, 2017 at 4:33

You can also work with mesh properties, like so:

pts = RandomReal[{-1, 1}, {25, 2}];
mesh = VoronoiMesh[pts];

areas = PropertyValue[{mesh, 2}, MeshCellMeasure];

 SetProperty[{mesh, {2, All}}, MeshCellStyle -> ColorData["Rainbow"] /@ Rescale[areas]],
 BarLegend[{"Rainbow", MinMax[areas]}]

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.