Function Repository Resource:

PlotClusters

Source Notebook

Cluster and plot two-dimensional points

Contributed by: Maximilien Tirard

ResourceFunction["PlotClusters"][points]

plot clusters in a list of two dimensional points.

ResourceFunction["PlotClusters"][points,n]

plot n clusters.

Details

Only works for 2D data.
There must be at least two regions.

Examples

Basic Examples (2) 

Identify and cluster points from some sample data:

In[1]:=
data = CompressedData["
1:eJxtUktqAkEQbcxgPoyJzoh7r2AO0H2GHEFItp5Hb5JV9zYQ7+BqIDALRZEM
iOjMe6+hxYKm6FdVr15X9XS++PjsGWNm19P61bK1nV//tpa5987eHPA/4kPe
B+GrsxdnOstjPvLGDvEh8ZL5r6pnfML7t0Xdk+oDfO2R1yPPY+QlHtQXOrIA
L56M+CSkus4WvuK7DuxfE++z7t/rndKPfnsrfuDjoDnBP7N/wXhlpUs+nVdO
XPlHq3lL3/39lPQP7mYfQTqAF3FfwMuQ6smjTvAbp3rwj5hv6DeWc/DEmd9Q
38mmOgZRL+4/Vnqkn//Aqz/fTf6M8VGcm3SLT/9I/wTxLffRsN/Ra856X8rf
eOm53e8FGjw92w==
"];
ResourceFunction[
 "PlotClusters", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][data]
Out[2]=

Find clusters in randomly generated data:

In[3]:=
data = RandomSample[Join[{-1, 1} + # & /@ RandomReal[{0, 1}, {20, 2}],
    {1, .5} + # & /@ RandomReal[{0, 1}, {20, 2}],
    {1, -1} + # & /@ RandomReal[{0, 1}, {20, 2}],
    {-.2, -.2} + # & /@ RandomReal[{0, 1}, {20, 2}]]];
ResourceFunction[
 "PlotClusters", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][data]
Out[4]=

Scope (1) 

Change the number of clusters:

In[5]:=
data = CompressedData["
1:eJxtUktqAkEQbcxgPoyJzoh7r2AO0H2GHEFItp5Hb5JV9zYQ7+BqIDALRZEM
iOjMe6+hxYKm6FdVr15X9XS++PjsGWNm19P61bK1nV//tpa5987eHPA/4kPe
B+GrsxdnOstjPvLGDvEh8ZL5r6pnfML7t0Xdk+oDfO2R1yPPY+QlHtQXOrIA
L56M+CSkus4WvuK7DuxfE++z7t/rndKPfnsrfuDjoDnBP7N/wXhlpUs+nVdO
XPlHq3lL3/39lPQP7mYfQTqAF3FfwMuQ6smjTvAbp3rwj5hv6DeWc/DEmd9Q
38mmOgZRL+4/Vnqkn//Aqz/fTf6M8VGcm3SLT/9I/wTxLffRsN/Ra856X8rf
eOm53e8FGjw92w==
"];
ResourceFunction[
 "PlotClusters", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][data, 4]
Out[6]=

Neat Examples (1) 

Observe how clustering changes as the number of clusters changes:

In[7]:=
data = CompressedData["
1:eJxtUktqAkEQbcxgPoyJzoh7r2AO0H2GHEFItp5Hb5JV9zYQ7+BqIDALRZEM
iOjMe6+hxYKm6FdVr15X9XS++PjsGWNm19P61bK1nV//tpa5987eHPA/4kPe
B+GrsxdnOstjPvLGDvEh8ZL5r6pnfML7t0Xdk+oDfO2R1yPPY+QlHtQXOrIA
L56M+CSkus4WvuK7DuxfE++z7t/rndKPfnsrfuDjoDnBP7N/wXhlpUs+nVdO
XPlHq3lL3/39lPQP7mYfQTqAF3FfwMuQ6smjTvAbp3rwj5hv6DeWc/DEmd9Q
38mmOgZRL+4/Vnqkn//Aqz/fTf6M8VGcm3SLT/9I/wTxLffRsN/Ra856X8rf
eOm53e8FGjw92w==
"];
plots = Table[
   ResourceFunction[
    "PlotClusters", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][data, n], {n, 2, 5}];
Manipulate[plots[[n]], {{n, 2}, 1, Length[plots], 1}]
Out[9]=

Publisher

Maximilien Tirard

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.1 – 26 March 2025
  • 1.0.0 – 24 March 2025

Related Resources

License Information