4
$\begingroup$

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}];
VoronoiMesh[pts]

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!

$\endgroup$
8
$\begingroup$
pts = RandomReal[{-1, 1}, {25, 2}];
v = VoronoiMesh[pts];
mp = MeshPrimitives[v, 2];
a = Rescale[Area /@ mp];
Legended[Graphics[
  MapThread[{ColorData["Rainbow"][#1], #2} &, {a, mp}]], 
 BarLegend["Rainbow"]]

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

enter image description here

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

You can also work with mesh properties, like so:

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

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

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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