You can define a customized math "function" which relies on the fact that it is evaluated while the input table is read. In other words, it is only valid within your context.
It uses the FPU of pgf which is used by pgfplots, and, in particular, its method \pgfmathfloatparsenumber
. A float in pgf has "flags" which is an integer with the meaning
0 == '0' (the number is +- 0.0),
1 == '+',
2 == '-',
3 == 'not a number'
4 == '+ infinity'
5 == '- infinity'
consequently, we can call \pgfmathfloatparsenumber
and use \pgfmathfloatgetflagstomacro
to access the flag. Integer comparisons can be done by means of \ifnum<int>=<int> \else \fi
.
Defining two functions, one for Y and one for the error bar results in
\documentclass[11pt]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{verbatim}
\begin{filecontents*}{data.dat}
time w1 e1 w2 e2
1 3019 40 nan nan
2 3045 34 nan nan
3 3100 50 3104 24
4 3500 13 3498 90
5 3800 90 3803 12
6 nan nan 3980 43
7 nan nan 3985 80
\end{filecontents*}
\begin{document}
\thispagestyle{empty}
% if (index 1 and index 3 are not NaN):
% y expr= (index 1 + index 3)/2
% y error expr= max{index 2, index 4}
% else if (index 3 = NaN): % && index 1 is not NaN
% y index =1
% y error index=2
% else if (index 1 = NaN): % && index 3 is not NaN
% y index =3
% y error index=4
\pgfmathdeclarefunction{VAL}{0}{%
\pgfmathfloatparsenumber{\thisrowno{1}}\let\A=\pgfmathresult
\pgfmathfloatparsenumber{\thisrowno{3}}\let\C=\pgfmathresult
\pgfmathfloatgetflagstomacro\A\flags
\ifnum\flags=3 %
\pgfmathfloatgetflagstomacro\C\flags
\ifnum\flags=3 %
% A == nan && C == nan
\let\pgfmathresult=\A
\else
% A == nan && C != nan
\let\pgfmathresult=\C
\fi
\else
\pgfmathfloatgetflagstomacro\C\flags
\ifnum\flags=3 %
% A != nan && C == nan
\let\pgfmathresult=\A
\else
% A != nan && C != nan
\pgfmathparse{(\A + \C)/2}%
\fi
\fi
}%
\pgfmathdeclarefunction{VALERR}{0}{%
\pgfmathfloatparsenumber{\thisrowno{1}}\let\A=\pgfmathresult
\pgfmathfloatparsenumber{\thisrowno{2}}\let\B=\pgfmathresult
\pgfmathfloatparsenumber{\thisrowno{3}}\let\C=\pgfmathresult
\pgfmathfloatparsenumber{\thisrowno{4}}\let\D=\pgfmathresult
\pgfmathfloatgetflagstomacro\A\flags
\ifnum\flags=3 %
\pgfmathfloatgetflagstomacro\C\flags
\ifnum\flags=3 %
% A == nan && C == nan
\let\pgfmathresult=\B
\else
% A == nan && C != nan
\let\pgfmathresult=\D
\fi
\else
\pgfmathfloatgetflagstomacro\C\flags
\ifnum\flags=3 %
% A != nan && C == nan
\let\pgfmathresult=\B
\else
% A != nan && C != nan
\pgfmathparse{max(\B,\D)}%
\fi
\fi
}%
\begin{tikzpicture}
\begin{axis} [
compat=newest,
y tick label style={/pgf/number format/1000 sep=},
ymin=2900,
ymax=4400]
\addplot [color=blue, thick,
error bars/.cd,
y explicit,
y dir=both,
] table [
x index=0,
y expr=VAL,
y error expr=VALERR,
]
{data.dat};
\addlegendentry{fit}
\end{axis}
\end{tikzpicture}
\end{document}

note that I have eliminated the two other \addplot
statements.
The \let\<macro>=<othermacro>
copies <othermacro>
to <macro>
. The constructions \ifnum<\macro>=3 %
compares two integers. Note the trailing space after 3
, it is important to tell TeX that it should stop to search for further numeric literals. It will be omitted from the output.
You can use restrict expr to domain
:
\begin{filecontents*}{mydata.dat}
C P1 P2 P3 sigP3
1 1.12E-4 0.06189 0.1865 0.0080
1 6.03E-3 0.00000 0.2517 0.0046
1 2.64E-2 0.00000 0.2247 0.0165
2 6.49E-5 0.00000 0.1906 0.0043
2 1.27E-5 0.00296 0.2120 0.0039
2 3.34E-4 0.00865 0.1709 0.0050
3 1.59E-2 0.01857 0.1596 0.0216
3 7.10E-4 0.01857 0.2547 0.0316
3 3.23E-5 0.00526 0.1557 0.0051
3 2.33E-4 0.01857 0.2008 0.0136
3 5.80E-4 0.01857 0.2389 0.0172
\end{filecontents*}
\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
legend cell align=right,
legend style={font=\footnotesize,legend pos=outer north east}}
\begin{semilogxaxis}[
enlargelimits=false,axis on top,
width=12cm,height=8cm,
xlabel={$\Pi_1$},
ylabel={$\Pi_3$},
ymin=0.1,ymax=0.35,
xmin=1E-5,xmax=1E-1,
log basis x=10,
]
\addplot+[only marks,error bars/.cd,y dir=both,y explicit]
table[x=P1,y=P3,y error=sigP3,
restrict expr to domain={\thisrow{C}}{1:1}
]{mydata.dat};
\addlegendentry{Cond. 1}
\addplot+[only marks,error bars/.cd,y dir=both,y explicit]
table[x=P1,y=P3,y error=sigP3,
restrict expr to domain={\thisrow{C}}{2:2}
]{mydata.dat};
\addlegendentry{Cond. 2}
\addplot+[only marks,error bars/.cd,y dir=both,y explicit]
table[x=P1,y=P3,y error=sigP3,
restrict expr to domain={\thisrow{C}}{3:3}
]{mydata.dat};
\addlegendentry{Cond. 3}
\end{semilogxaxis}
\end{tikzpicture}
\end{document}

Or with \pgfplotsinvokeforeach
\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
legend cell align=right,
legend style={font=\footnotesize,legend pos=outer north east}}
\begin{semilogxaxis}[
enlargelimits=false,axis on top,
width=12cm,height=8cm,
xlabel={$\Pi_1$},
ylabel={$\Pi_3$},
ymin=0.1,ymax=0.35,
xmin=1E-5,xmax=1E-1,
log basis x=10,
]
\pgfplotsinvokeforeach{1,2,3}{
\addplot+[only marks,error bars/.cd,y dir=both,y explicit]
table[x=P1,y=P3,y error=sigP3,
restrict expr to domain={\thisrow{C}}{#1:#1}
]{mydata.dat};
\addlegendentry{Cond. #1};
}
\end{semilogxaxis}
\end{tikzpicture}
\end{document}
Best Answer
You can use
mark repeat={<integer>}
, where the positive<integer>
causes to draw only each n-th mark. A little example: