Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have four text files with each 21 columns, and I want to plot, for each file, the first column as x, and then the 20 next columns as y. For each file, then put it all together on the same graph. It's basically 20 replicates under 4 different conditions.

I have the following code, using \foreach, but I'd like to make it cleaner by using \addplot+ rather than \addplot with the fill argument. If I use \addplot+ within the \foreach loop, then each replicate has it's own color, and it's not really what I want.

To be clear, the following code does what I want, but I'd like to hear about solutions to make it nicer.

\documentclass[professionalfonts,11pt]{beamer}
\begin{document}
\begin{frame}{Conséquences des variations du taux de croissance}

\begin{center}
    \begin{tikzpicture}
        \begin{semilogyaxis}[
            xlabel=Temps,
            ylabel={Taille de population},
            cycle list name = monokai,
            legend pos = north west
            ]
            \foreach \yindex in {2,...,20}
                \addplot[mark = none, draw = RYB1] table [y index = \yindex] {data/vardem_30.dat};
            \foreach \yindex in {2,...,20}
                \addplot[mark = none, draw = RYB2] table [y index = \yindex] {data/vardem_10.dat};
            \foreach \yindex in {2,...,20}
                \addplot[mark = none, draw = RYB3] table [y index = \yindex] {data/vardem_3.dat};
            \foreach \yindex in {2,...,20}
                \addplot[mark = none, draw = RYB4] table [y index = \yindex] {data/vardem_1.dat};
        \end{semilogyaxis}
    \end{tikzpicture}
\end{center}

\end{frame}
\end{document}
share|improve this question
    
I don't see the question here. Do you want to simplify the code or what kind of niceness are you after? You can put 4 \addplot commands under one \foreach. Is that what you mean by niceness? You can find the explanation for using + in the manual. –  percusse Aug 26 '12 at 10:11

1 Answer 1

up vote 8 down vote accepted
  • Your example is missing \usepackage{pgfplots}
  • You don't neccessarily need draw=color, just color is enough
  • As percusse said, one foreach loop should be enough
  • You said you had 21 colums, so the loop should run to that value
  • \addplot[options] should only execute the options, so you can leave out the mark=none. Otherwise you may specify \pgfplotsset{every axis plot post/.append style={mark=none}}

\documentclass[professionalfonts,11pt]{beamer}
\usepackage{pgfplots}

\begin{document}

\begin{frame}{Conséquences des variations du taux de croissance}
    \begin{center}
        \begin{tikzpicture}
          \begin{semilogyaxis}
          [ xlabel=Temps,
            ylabel={Taille de population},
            cycle list name = monokai,
            legend pos = north west,
            ]
          \foreach \yindex in {2,...,21}
            {   \addplot[RYB1] table [y index = \yindex] {data/vardem_30.dat};
            \addplot[RYB2] table [y index = \yindex] {data/vardem_10.dat};
            \addplot[RYB3] table [y index = \yindex] {data/vardem_3.dat};
            \addplot[RYB4] table [y index = \yindex] {data/vardem_1.dat};
            } 
          \end{semilogyaxis}
        \end{tikzpicture}
    \end{center}
\end{frame}

\end{document}
share|improve this answer
1  
You can also factorize the four \addplot with \foreach \name/\file in {RYB1/30,RYB2/10.... –  Paul Gaborit Aug 26 '12 at 12:40
    
Thanks! This is really simpler than my code... (for some reason, I'm still thinking in C loops, so I'm used to them starting from 0...) –  Timothée Poisot Aug 26 '12 at 17:26
    
I know what you mean ;) Also, for \foreach \x [count=\c] in {red,blue,white} here \c starts to count from 1, which was irritating the first time I used it. –  Tom Bombadil Aug 26 '12 at 17:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.