0

to this question (pgfplots(table) spreadsheet like formulas) I got the comment that it could be done with the package datatool. But actually I have problems passing the data from datatool to pgfplots.

Things like this:

    \documentclass[border=3pt]{standalone}
    \usepackage{datatool}
    \usepackage{pgfplots}


    \catcode`\^^I=12 %
    \DTLsetseparator{       }%

    \pgfplotsset{compat=1.12}
    \DTLloaddb{coordinates}{data.csv}

    \begin{document}

    \begin{tikzpicture}
    \begin{axis}[
    ytick={5,6,7},
    xtick=data
    ]

            \DTLforeach*{coordinates}{\x=x,\y=y}{%
                    \ifnum\value{DTLrowi}>1
                    \addplot coordinates {(\mtx,\mty) (\x,\y)};
                    \fi
                    \xdef\mtx{\x}\xdef\mty{\y}
                    }

    \end{axis}
    \end{tikzpicture}


    \end{document}

Do not work properly, since I get always two points for one curve, then a new totally independent curve is drawn. This is logically absolute not what I intend to do. My aim is to rea the data from the file, process it with datatool and plot them then with pgfplots (yes I know there is datatoolplot, but pgfplots is more flexible).

Furthermore a loop with an if to insert the data to pgfplots seems very long winded to me.

Can anyone think of a better more simple way to achieve this (and especially pass all data to one plot, not as many lines as in the files are (minus one))?

EDIT: Data file may look like this (tab separated):

x    y
1    2
2    4
3    2
4    0
  • 1
    Please make your question complete and self-contained. In the previous question you got lucky, and someone created the data file for you. This question is very sensitive to the precise way your data file looks like, so it is crucial to provide it. – Schrödinger's cat Mar 21 at 0:16
  • Sorry, added an example for the data file – atticus Mar 21 at 1:16
  • 3
    Thanks for adding the data file! However, I do not understand the question. You can plot the data file right away with pgfplots. You do not need datatool at all. Please post the question in such a way that others understand why you are doing what you are doing. – Schrödinger's cat Mar 21 at 1:19
  • 1
    @Marijn It would still be good to see an explicit example which illustrates this, i.e. an example in which the datatool + pgfplots combination is great and easy, and the pure pgfplots solution sucks. I may not know datatool well enough, but I do not see that without an explicit case. (BTW, forget plot also suppresses the legend. Also by patching the plots together like that you lose access to important features like adding a smooth key, having cool functions of \coordindex, and so on. While I upvoted many of your answers I won't upvote this one for that reason.) – Schrödinger's cat Mar 21 at 16:08
  • 1
    @Schrödinger'scat I added a version with a single \addplot command, as in tex.stackexchange.com/questions/266332/…. – Marijn Mar 22 at 9:45
1

You can forget each plot, so the next line will have the same style as the first. If you want to add a legend then you should provide a separate entry for that with \addlegendimage, because all previous plots are forgotten so there is nothing to add a legend entry for. But indeed it is not very efficient, so if you are able to do the necessary data manipulation within pgfplots then that is recommended. Also note the + in \addplot+ that is needed to apply automatic formatting (color, markers) to a forget plot plot. I have replaced the tabs by comma's in the input file to simplify the code a bit.

MWE:

\documentclass{article}
\usepackage{datatool}
\usepackage{pgfplots}

\DTLloaddb{coordinates}{data.csv}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
ytick={0,...,4},
xtick=data
]
\DTLforeach*{coordinates}{\x=x,\y=y}{%
     \ifnum\value{DTLrowi}>1
     \addplot+[forget plot] coordinates {(\mtx,\mty) (\x,\y)};
     \fi
     \xdef\mtx{\x}\xdef\mty{\y}
}
\addlegendimage{line legend,blue,mark=*}
\addlegendentry{x-y plot}
\end{axis}
\end{tikzpicture}

\end{document}

Result:

enter image description here


Edit: to avoid a separate \addplot for each coordinate you can first store the list of coordinates in a loop and then use a single plot command. This is a simplified version of Iterating through a table using values on the next row. The output is the same.

\documentclass{article}
\usepackage{datatool}
\usepackage{pgfplots}

\DTLloaddb{coordinates}{data.csv}

\begin{document}

% adapted from https://tex.stackexchange.com/questions/266332/iterating-through-a-table-using-values-on-the-next-row
\def\mtplot{}
\DTLforeach*{coordinates}{\x=x,\y=y}{%
\edef\mtplot{\mtplot(\x,\y)}%
}

\begin{tikzpicture}
\begin{axis}[
ytick={0,...,4},
xtick=data
]
\addplot coordinates {\mtplot};
\addlegendentry{single plot}
\end{axis}
\end{tikzpicture}

\end{document}
| improve this answer | |
  • Hm, ok than it seams that it's not possible to do what and making it clean (without generating a lot of addplot commands). Nevertheless thanks for your answer ;) – atticus Mar 21 at 15:59
  • @atticus Well, pgfplots doesn't mind executing all the \addplots, it will probably still be fast (unless you have thousands of points). – Marijn Mar 21 at 16:03
  • Yes of course but its somewhat not clean ;). (Its not a matter of never using this for me, its just a matter of now knowing that its not possible otherwise I'll try to prevent as often as I can to use this) – atticus Mar 21 at 16:07
  • 1
    @atticus see edit for a cleaner version, which was also in the 2015 answer but I didn't look close enough to see that this has a clear advantage over the multiple \addplot approach. – Marijn Mar 22 at 9:47

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.