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 created the following code that appears to do what I want it to. Namely add an additional row to the table \IDA that is the average of four values. This row can then be used for a plot in tikzpicture.

\documentclass[tikz]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots, pgfplotstable}

\begin{filecontents}{FileA.dat}
step ts1   ts2
1   3 4
2   7 6
3   4 6
4   5 6
5   5 7
6   9 3
\end{filecontents}

\begin{filecontents}{FileB.dat}
step  tHIws
1   3
2   7
3   4
4   5
5   5
6   9
\end{filecontents}

% for calculations 
\pgfplotstableread{FileB.dat}\SST   
\pgfplotstableread{FileA.dat}\IDA
\pgfplotstableset{
    create on use/tHIws/.style={
        create col/expr={\thisrow{ts1}+\thisrow{ts2})/2}
    }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
        axis x line=bottom,
        axis y line=left,
        tick align=outside,
        xlabel=\texttt{step},
        ylabel={$\theta_{a}$},
        legend pos= north west,
        legend columns=6,
        legend style={fill=none, draw=none},
        cycle multi list={
                color list%
        },
        ymajorgrids,
]
\addplot table
        [x=step, y=tHIws] 
        {\SST};
    \addlegendentry{SSTe}       
\addplot +[mark=*] table
        [x=step, y=tHIws] 
        {\IDA};
    \addlegendentry{IDA}        
\addplot table
        [x=step, y expr=(\thisrow{ts1}+\thisrow{ts2})/2] 
        {FileA.dat};
    \addlegendentry{IDA}        
\end{axis}  

\end{tikzpicture}
\end{document}

As I will however need to do this for a number of tables/figures I would like to somehow combine the two commands into one or at least make sure that the above \pgfplotstableset in only applied to the table \IDA.

Is there a way to do this?


Edited due to percusse's answer. I have actually tried to transfer that to tikxpictures but failed so far.


Edit 2: Full example!


Edit 3: Now with internal data. However I get an error on compiling: "Could not retrieve column 'step' from table 'FileA.dat'." But I can't seem to spot the error - I guess I am "Betriebsblind"


Edit 4: Added the missing usepackage for the inline file content

share|improve this question
    
Hi I'm trying to understand the pgfplots issue a bit better. Would it be a fair solution if you would write [mymedian={ts1,ts2,ts3}] or should it be something else? My main difficulty in understanding is that what is being repeated over number of plots throughout the document, the mean calculation or the column names? –  percusse May 18 at 9:40
    
In my real document I don't only have FileA.dat and FileB.dat but rather about 50+ of them. All of them (which are like file A) have some columns I would like to have the average of (not the median). The columns all have the same names '(ts1, ..., tsN)'. As the averaging is the same for all tables I would like to have something that I can just apply to any of the tables and be sure what table I applied it to. As it is in my example I just use '\pgfplorstableset' after I have loaded a table and thats the one it is applied to. I would like to combine both into one action as to not mix it up. thx –  MatoBehr May 18 at 13:11

1 Answer 1

up vote 4 down vote accepted

Regarding the edit, it is easier to place the experssion into a style and apply it whenever needed. Here I called it mymedian

\documentclass{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.10}
\pgfplotstableread{
step ts1 ts2 ts3 ts4
1    3   4   11   14
2    7   6   22   26
3    4   6   34   31
4    5   6   45   46
5    5   7   55   57
6    9   3   69   63
}\SST

% for calculations 
\pgfplotsset{mymedian/.style={
  x=step, y expr=(\thisrow{ts1}+\thisrow{ts2}+\thisrow{ts3}+\thisrow{ts4})/4
  }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
        axis x line=bottom,
        axis y line=left,
        tick align=outside,
        xlabel=\texttt{step},
        ylabel={$\theta_{a}$},
        legend pos= north west,
        legend columns=6,
        legend style={fill=none, draw=none},
        cycle multi list={
                color list%
        },
        ymajorgrids,
]
\addplot table[mymedian] {\SST};
    \addlegendentry{SSTmean}       
\addplot table[x=step,y=ts3] {\SST};
    \addlegendentry{SSTS3}       
\end{axis}  
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
I have just realised that my question was badly formulated as I am talking about tables. I have in fact been using something similar (but not as elegant) for that purpose. I have now edited the original question to reflect that I wanted to use this for a tikzpicture with axis rather than a table. –  MatoBehr May 8 at 19:07
    
@MatoBehr Can you include the whole example from documentclass to \end{document}? That saves us quite some time. –  percusse May 8 at 19:14
    
The question now contains a working example –  MatoBehr May 12 at 7:52
    
@MatoBehr Ooops. So sorry about this. I completely forgot and it got lost in the inbox. I'll check this, this evening (afternoon now). –  percusse May 16 at 12:37
    
Great, I would really appreciate it. –  MatoBehr May 17 at 13:10

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.