Tikz - 绘制聚类图

Tikz - 绘制聚类图

我如何创建像图中这样的聚类模式?

聚类模式

\documentclass{article}

\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}

    \end{tikzpicture}
\end{document}

答案1

在此处输入图片描述

将此视为Asymptote MWE一个起点:

// cluster.asy
//
// run asy cluster.asy
//
// to get cluster.pdf

settings.tex="pdflatex";    
import graph;
import math;

size(12cm,9cm);
import fontsize;defaultpen(fontsize(9pt));
texpreamble("\usepackage{lmodern}"+"\usepackage{amsmath}"
+"\usepackage{amsfonts}"+"\usepackage{amssymb}"
);

real lineW=0.8bp;
real dotW=3bp;

pen shapePen0=darkblue+lineW;
pen shapePen1=deepred+lineW;
pen shapePen2=deepgreen+lineW;
pen dotPen0=shapePen0+dotW;
pen dotPen1=shapePen1+dotW;
pen dotPen2=shapePen2+dotW;

struct Cluster{
    guide shape;
    int ndots;
    pair[] dots;
    pen shapePen,dotPen;

    void genDots(){
        pair ll=min(shape);
        pair ur=max(shape);
        guide bb=box(ll,ur);
        real w=abs(ll.x-ur.x);
        real h=abs(ll.y-ur.y);
        pair p;
        int n=0;
        while(n<ndots){
            p=shift(ll)*scale(w,h)*(unitrand(),unitrand());
            if(inside(shape,p)){
                dots.push(p);
                ++n;
            }
        }
        draw(shape,shapePen);
        dot(dots,dotPen);
    }

    void operator init(guide shape,explicit int ndots
    ,pen shapePen=currentpen, pen dotPen=currentpen
    ){
        this.shape=shape;
        this.ndots=ndots;
        this.dots=new pair[];
        this.shapePen=shapePen;
        this.dotPen  =dotPen;
        genDots();
    }
}

pair[] Distance0={  
    (29, 22),(25, 24),(23, 26),(20, 31),(18, 37),(12, 37),(8, 37),
    (6, 35),(6, 33),(6, 29),(4, 21),(5, 17),(10, 15),(12, 13),
    (16, 12),(20, 14),(21, 18),(25, 18),(28, 19),(30, 20),
};

pair[] Distance1={  
    (46, 31),(42, 40),(34, 42),(28, 42),(25, 39),(22, 36),
    (22, 31),(26, 27),(29, 26),(32, 24),(39, 25),(44, 28),
};
pair[] Distance2={  
    (59, 23),(55, 23),(53, 25),(49, 23),(42, 22),(41, 19),(42, 10),
    (44, 9),(45, 4),(47, 2),(52, 3),(55, 5),(56, 8),(58, 11),(60, 16),
};

pair[] Density0={   
    (43,28),(40,33),(38,37),(33,40),(26,41),(21,39),(16,40),
    (16,37),(16,34),(19,29),(26,26),(34,25),(39,26),
};

pair[] Density1={
    (61,22),(60,24),(51,26),(41,25),(29,22),(16,30),(14,36),(11,37),
    ( 7,37),( 1,24),( 4,12),(11,10),(26, 4),(43, 3),(50, 6),(57,13),
};

pair[] Gest0={
    (44, 37),(37, 36),(33, 34),(31, 30),(32, 25),(35, 22),(38, 22),
    (41, 20),(45, 17),(46, 14),(45, 11),(43, 8),(38, 6),(33, 5),(29, 5),
    (29, 4),(30, 4),(34, 4),(39, 5),(43, 7),(47, 10),(48, 14),(46, 18),(43, 21),
    (39, 23),(36, 24),(33, 26),(33, 29),(35, 33),(38, 34),(44, 36),(44, 36),
};

guide[] g={ // all shapes
    graph(Distance0,operator..)..cycle,
    graph(Distance1,operator..)..cycle,
    graph(Distance2,operator..)..cycle,

    shift(2.5cm,0)*graph(Density0,operator..)..cycle,
    shift(2.5cm,0)*graph(Density1,operator..)..cycle,

    shift(2*2.5cm,0)*graph(Gest0,operator..)..cycle,  
    shift(2*2.5cm,0)*box((5, 1),(58, 2)),
};

srand(1456477);

Cluster[] Distance={
    Cluster(g[0],20,shapePen0,dotPen0),
    Cluster(g[1],20,shapePen1,dotPen1),
    Cluster(g[2],20,shapePen2,dotPen2),
};

Cluster[] Density={
    Cluster(g[3],20,shapePen0,dotPen0),
    Cluster(g[4],3*20,shapePen1,dotPen1),
};

Cluster[] Gest={
    Cluster(g[5],20,shapePen0,dotPen0),
    Cluster(g[6],20,shapePen1,dotPen1),
};

real dotw=3bp;

pair ul=point(currentpicture,plain.NW);
pair ur=point(currentpicture,plain.NE);

label("Distance", ((5*ul+ur).x/6, ul.y+3mm));
label("Density" , (  (ul+ur).x/2, ul.y+3mm));
label("Shape"   , ((ul+5*ur).x/6, ul.y+3mm));

drawline((2.3cm,0),(2.3cm,0)+plain.N,gray(0.6)+1bp);
drawline((5cm,0),(5cm,0)+plain.N    ,gray(0.6)+1bp);

相关内容