I have generated a dataset that clustered some of data points into three groups, the output was three -column data like this:

enter image description here

there are some points with X,Y coordinates and I already labeled each of them the group number (from 1-3).

As I want to visualize the result by demonstrating those points on a Cartesian plane, for each group of points using the different color so that people can see the clustering result clearly. Something like this below that plotted by Mathematica ListPlot[FindCluster[]]:

(this pic has showed four groups of data (so 4 types of color), in my case, i just need three kind of whatever color)

I've used Riffle[] and Partition[] to pair the points' x,y coordinates accordingly. So by ListPlot[] them you can see those points are landing correctly:

enter image description here

So basically I came up with Mathematica when I firstly had some thinking about demonstrating my result. But the tricky part is how could I show different colors for each group. Looks like FindCluster[] could do the most of the part job like this (or I could be wrong), but I might still need one or two more steps to color the points according to my group labels.

Please feel free to leave your comment, I appreciate your help!


2 Answers 2


If you already have your points labeled by group, I'm not totally sure I understand how FindClusters is helpful here, but please correct me if I've missed something.

In any case, you can split up the data into separate lists (by group). ListPlot will color points in different lists differently (and of course, you can customize the styling):

data = {RandomReal[{-1, 1}], RandomReal[{-1, 1}], 
     RandomInteger[{1, 3}]} & /@ Range[100];

gathered = GatherBy[data, #[[3]] &];

 #[[All, 1 ;; 2]] & /@ gathered,
 PlotStyle -> {Red, Blue, Green}


Hope that helps!

  • $\begingroup$ Thank you, you are right, if the group is labeled, no need for the FindClusters at this stage. Just ListPlot the groups, thank you for your help! $\endgroup$
    – leon365
    Dec 5, 2017 at 3:34

You can also use GroupBy:

For example, data in form $\{...,\{x_i,y_i,lbl_i\},...\}$.

Dummy data:

data = Join @@ 
   MapIndexed[Function[{u}, Append[u, #2[[1]]]] /@ #1 &, 
    RandomVariate[BinormalDistribution[#, {1, 1}, 0.03], 100] & /@ 
     RandomInteger[{-3, 3}, {4, 2}]];


g = KeySort[GroupBy[data, Last, #[[All, {1, 2}]] &]];
With[{k = Keys[g]}, ListPlot[k /. g, PlotLegends -> k]]

enter image description here


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