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.

Basically I'm plotting many curves stacked one after the other in pseudo 3D to show transient behaviors. I need every x axis to be at the same place so I use a ymin and ymax optional entry for the axis environment. So far I used a variable that I changed by hand, but I'm wondering if there is a way to find a min and max from several external tables (18 here) and use it as a variable in my case.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

You can use \pgfplotstablevertcat{<newmacro>}{<filename>} to concatenate your datatables, then use \pgfplotstablesort{<newmacro>}{<table macro>} to sort the concatenated table, and get the largest (or smallest) value by extracting the first element of the sorted table using \pgfplotstablegetelem{<row>}{<col>}\of{<table macro>}.

Below are two scripts, \findmax and \findmin that take a comma separated list of filenames, concatenate the tables in those files and return the maximum/minimum.

finding extreme points

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\newcommand{\findmax}[1]{
    \pgfplotsforeachungrouped \table in {#1} {%
        \pgfplotstablevertcat{\concatenated}{\table}%
    }%
    \pgfplotstablesort[sort key={1},sort cmp={float >}]{\sorted}{\concatenated}%
    \pgfplotstablegetelem{0}{1}\of{\sorted}%
    \let\ymax=\pgfplotsretval%
}

\newcommand{\findmin}[1]{
    \pgfplotsforeachungrouped \table in {#1} {%
        \pgfplotstablevertcat{\concatenated}{\table}%
    }%
    \pgfplotstablesort[sort key={1},sort cmp={float <}]{\sorted}{\concatenated}%
    \pgfplotstablegetelem{0}{1}\of{\sorted}%
    \let\ymin=\pgfplotsretval%
}

\begin{filecontents}{dataA.dat}
1 5
2 3
3 6
4 4
5 0
6 1
\end{filecontents}

\begin{filecontents}{dataB.dat}
1 2
2 3
3 2
4 2
5 3
6 2
\end{filecontents}

\begin{filecontents}{dataC.dat}
1 -2
2 0
3 -1
4 -1
5 -2
6 0
\end{filecontents}



\begin{document}

\findmax{dataA.dat,dataB.dat,dataC.dat}
\findmin{dataA.dat,dataB.dat,dataC.dat}

\pgfplotstabletypeset{dataA.dat} \hfill \pgfplotstabletypeset{dataB.dat} \hfill \pgfplotstabletypeset{dataC.dat} \hspace{4cm}\\[0.5cm]

\noindent
Maximum value: \ymax\\
Minimum value: \ymin\\[0.5cm]

\pgfplotsset{ymin=\ymin,ymax=\ymax,width=3cm,scale only axis}
\begin{tikzpicture}
\begin{axis}[at={(0,0)}]
\addplot table {dataA.dat};
\end{axis}

\begin{axis}[at={(4cm,0)}]
\addplot table {dataB.dat};
\end{axis}

\begin{axis}[at={(8cm,0)}]
\addplot table {dataC.dat};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
thanks a lot jake, exactly what I needed. –  ben paillard Aug 5 '11 at 13:26
add comment

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.