章鱼图 tikz/pgfplots

章鱼图 tikz/pgfplots

编辑:我找到了解决方案。见下文。

我正在尝试得到这样的东西:在此处输入图片描述

我有小点和大点,一些小点连接到一个大点。我可以用 pgfplots “绘制”所有点:

\addplot[scatter,
        only marks,
        point meta=explicit symbolic,
        scatter/classes={
        a={mark=square*,blue},%
        b={mark=triangle*,red},%
        d={mark=triangle*,green},%
        c={mark=o,draw=black}}]
         table[x=xs, y=ys, meta=qs] {test.txt};
    \end{axis}

但我不知道如何添加单个点之间的连接。

我正在寻找这样的东西:

\foreach \x1/\y1/\x2/\y2 in data{
\draw (\x1,\y1) -- (\x2,\y2); 
} 

或者有更好的办法吗?顺便说一句:所有数据都是我自己生成的,因此更改格式没有问题。

编辑:A MWE

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[]
\addplot[scatter,
        only marks,
        point meta=explicit symbolic,
        scatter/classes={
        a={mark=square*,blue},%
        b={mark=triangle*,red},%
        d={mark=triangle*,green},%
        c={mark=o,draw=black}}]
         table[x=xs, y=ys, meta=qs] {testoutput.txt};
%add here?
        \end{axis}
%or here?
\end{tikzpicture}
\end{document}

使用 testoutput.txt

xs xs ys qs
155 725 d 
195 1215 c 
1095 825 c 
245 2665 b 
395 2850 d 
1670 2875 t 
15 2660 t 
270 1960 t
1715 3010 t
1085 1110 t
1040 685 t
1985 1535 t
580 170 t
2250 2025 t
1815 2925 t
1420 3370 t
160 1125 t
25 500 t
460 640 t
100 70 t
375 170 t
1185 360 t

然后还有另一个文件 connection.dat

155 725 1670 2875
155 725 270 1960
155 725 100 70
155 725 1185 360
1095 825 715 2660
1095 825 1715 3010
1095 825 580 170

在生成数据之前,我不确定必须建立哪些连接。

编辑2:

     \DTLloaddb[noheader=false]{conns}{connections.dat}
     \DTLforeach*{conns}{\xA=x, \yA=y, \xB=a, \yB=b}{%
            \addplot[
                color=gray]
                coordinates {
                (\xA,\yA )(\xB,\yB)         
                };  }

但现在我遇到了一点性能问题。我的数据很大:每个图大约有 1000 个点和 ~500 个连接!我需要绘制其中的 8 个。编译我的 tex 的时间超过 1.5 分钟!有没有办法“预编译”图表?

答案1

您可以connections.dat使用scatter绘图样式和几个visualization depends on语句来绘制文件:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{filecontents}{testoutput.txt}
xs ys qs
155 725 d 
195 1215 c 
1095 825 c 
245 2665 b 
395 2850 d 
1670 2875 t 
15 2660 t 
270 1960 t
1715 3010 t
1085 1110 t
1040 685 t
1985 1535 t
580 170 t
2250 2025 t
1815 2925 t
1420 3370 t
160 1125 t
25 500 t
460 640 t
100 70 t
375 170 t
1185 360 t
\end{filecontents}


\begin{filecontents}{connections.dat}
155 725 1670 2875
155 725 270 1960
155 725 100 70
155 725 1185 360
1095 825 15 2660
1095 825 1715 3010
1095 825 580 170
\end{filecontents}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[]
\addplot[scatter,
        only marks,
        point meta=explicit symbolic,
        scatter/classes={
        a={mark=square*,blue},%
        b={mark=triangle*,red},%
        d={mark=triangle*,green},%
        c={mark=o,draw=black}}]
         table[x=xs, y=ys, meta=qs] {testoutput.txt};
\addplot [
    scatter, only marks, mark=none,
    visualization depends on=\thisrowno{0}\as\startx,
    visualization depends on=\thisrowno{1}\as\starty,
    visualization depends on=\thisrowno{2}\as\endx,
    visualization depends on=\thisrowno{3}\as\endy,
    scatter/@pre marker code/.code={
        \draw (0,0) -- (axis direction cs:{\endx-\startx},{\endy-\starty});
    },
    scatter/@post marker code/.code={},
] table {connections.dat};
        \end{axis}
\end{tikzpicture}
\end{document}

答案2

我的解决方案与包datatool

\documentclass{standalone}
\usepackage{pgfplots}    
\usepackage{datatool}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[]
\addplot[scatter,
        only marks,
        point meta=explicit symbolic,
        scatter/classes={
        a={mark=square*,blue},%
        b={mark=triangle*,red},%
        d={mark=triangle*,green},%
        c={mark=o,draw=black}}]
         table[x=xs, y=ys, meta=qs] {testoutput.txt};


     \DTLloaddb[noheader=false]{conns}{connections.dat}
     \DTLforeach*{conns}{\xA=x, \yA=y, \xB=a, \yB=b}{%
            \addplot[color=gray]
                coordinates {
                (\xA,\yA )(\xB,\yB) };  
            }

        \end{axis}
\end{tikzpicture}
\end{document}

相关内容