I am currently writing an MSc thesis involving unsupervised learning/clustering. I am wishing to illustrate the basic concepts of clustering using a figure. The figure I wish to draw should look somewhat like this image from Wikipedia: https://en.wikipedia.org/wiki/Cluster_analysis#/media/File:KMeans-Gaussian-data.svg

How can I draw a similar figure in LaTeX?

Thanks in advance.

  • 1
    Welcome to TeX.SX! This should be possible with pgfplots. – user31729 Apr 19 '16 at 8:22
  • pgfplots can certainly do it. For a start, take a look at section 3.4.2 Scatter Plot Use–case B in the manual for pgfplots. (Open a command prompt/terminal and write texdoc pgfplots to open the manual.) – Torbjørn T. Apr 19 '16 at 8:35

Metapost provides a normaldeviate command that will give you Gaussian random numbers that you can add to a given coordinate. Here's a way to use it to draw clusters.

enter image description here

prologues := 3;
outputtemplate := "%j%c.eps";

% draw n random points centered on Z with r scattering 
vardef draw_cluster(expr Z, n, r, marker) = 
   for i=1 upto n:
      draw marker shifted Z shifted (r*normaldeviate, r*normaldeviate);


u = 1cm;
z1 = (2u, 6.4u);
z2 = (u, 4.2u);

picture mark[];
mark1 = image(draw fullcircle scaled 3 withcolor .67 blue);
mark2 = image(draw fullcircle scaled 3 withcolor .67 red);

randomseed := 1701;

draw_cluster(z1, 32, 0.8u, mark1);
draw_cluster(z2, 24, 0.3u, mark2);


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.