3

EDIT:I found a solution. See below.

i'm trying to get something like this:enter image description here

I have small dots and big dots, and some small dots are connected to one big dot. I can "plot" all points with 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}

But i can't figure out, how to add the connection between single dots.

I'm looking for something like this:

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

Or is there a better way? Btw: All data are generated by myself, therefore it is no problem to change the format.

EDIT: 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}

with 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

And then there is an other file 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

It is not sure, which connections i have to made, not until the data is generated.

EDIT2:

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

But now i have a little perfomance problem. My datas are big: around 1000 dots and ~500 connections per graph! And i need to plot 8 of them. The time to compile my tex is more than 1.5 minutes! Is there a way to "precompile" the graphs?

5
  • Can you add a sample of the generated data and a full MWE? You can do exactly what you want, excepting of course the need to use \x, \y, \a, and \b (say) because you can't have digits in csnames :) Sep 16, 2014 at 22:34
  • I think my only problem is to read correctly from my connection.dat file.
    – ChuckZee
    Sep 17, 2014 at 7:27
  • How do you decide which connections to be drawn or to which point?
    – percusse
    Sep 17, 2014 at 10:11
  • This is quite complex. Let say, it's random. Btw: i found a solution.
    – ChuckZee
    Sep 17, 2014 at 11:23
  • 1
    @ChuckZee Don't be this guy: xkcd.com/979; post your solution as an answer :) Sep 17, 2014 at 11:55

2 Answers 2

4

You can plot the connections.dat file using the scatter plot style and a couple of visualization depends on statements:

\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
  • Thanks. I found a solution by myself. I will try yours as soon as possible.
    – ChuckZee
    Sep 17, 2014 at 11:27
  • @ChuckZee: I don't think it's a good idea to accept this answer if you haven't even tried it out. Could you please post your own solution as an answer and accept that?
    – Jake
    Sep 17, 2014 at 13:00
1

My solution with the package 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}
6
  • Instead of using one \addplot command per connecting line, I would recommend using \draw [gray] (axis cs:\xA,\yA) -- (axis cs:\xB,yB);. That is much more efficient than sending every line through the whole plot processing mechanism.
    – Jake
    Sep 17, 2014 at 13:22
  • I don't understand the problem. You are connecting each point with the previous point. But in your question you want them to connect to a common point.
    – percusse
    Sep 17, 2014 at 13:44
  • @percusse: I think ChuckZee is assuming a connections.dat file like the one in his question, where each line contains two coordinates (xA, yA, xB, yB), and the first coordinate would be the common points (in the example that would be (155,725) and (1095,825))
    – Jake
    Sep 17, 2014 at 14:11
  • @Jake That's pretty redundant I would say, before anything reading two extra columns with the identical info for all rows
    – percusse
    Sep 17, 2014 at 14:14
  • @percusse: I agree. They're not necessarily all identical, though. In ChuckZee's example image, there are six different common points, for example.
    – Jake
    Sep 17, 2014 at 14:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .