Let's start with the Voronoi diagram with color example from the documentation. It shows using the index i as the color argument to patch. What we need to do is replace that with the area of the polygon. The polyarea function will compute that. It takes the same X & Y coordinates that patch wants. So it simply looks like this: x = gallery('uniformdata',[10 2],5);
[v,c] = voronoin(x);
for i = 1:length(c)
if all(c{i}~=1)
x = v(c{i},1);
y = v(c{i},2);
a = polyarea(x,y);
patch(x,y,a);
end
end
Here's a more interesting example:
rng default
figure('Position',[100 100 500 600])
pts = randn(500,2);
[v,c] = voronoin(pts);
for i = 1:length(c)
if all(c{i}~=1)
x = v(c{i},1);
y = v(c{i},2);
a = polyarea(x,y);
patch(x,y,a);
end
end
hold on
scatter(pts(:,1),pts(:,2),40,[.9 .1 .1],'Marker','+','MarkerEdgeAlpha',.5)
colorbar('SouthOutside')
axis equal
xlim([-3 3])
ylim([-3 3])
caxis([0 2])
Best Answer