I am trying to filter a table in pgfplots as shown below. It does not compile and I guess this is because it is unable to correctly compare the strings. This example is based on the used guide. For information, I am using luatex, so I need a solution compatible with lualatex compiler please.
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
% Preamble: \pgfplotsset{width=7cm,compat=1.18}
\begin{tikzpicture}
\begin{axis}[grid=major]
\addplot+ [
unbounded coords=discard,
x filter/.expression={\thisrow{type}=="foo" ? x : nan},
] table[trim cells=true] {
x y type
1 2 "foo"
1.5 2.5 "bar"
2 3 "foo"
2.5 3.5 "bar"
3 4 "foo"
};
\end{axis}
\end{tikzpicture}
\end{document}
I get the following error:
! Package PGF Math Error: Could not parse input '@@str@@:foo' as a floating poi
nt number, sorry. The unreadable part was near '@@str@@:foo'. (in '"foo"=="foo"
? x : nan').
Note that if I replace my code by:
\addplot+ [
unbounded coords=discard,
x filter/.expression={\thisrow{type}==42 ? x : nan},
] table[trim cells=true] {
x y type
1 2 42
1.5 2.5 5
2 3 42
2.5 3.5 42
3 4 12
};
Then it works as expected, i.e., coords x = 1, 2, and 2.5 are plotted while 1.5 is skipped and 3.5 too (x axis stopped at 2.5).
How to compare strings? I found nothing in the user guide...
You could use a .code type of filter:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\def\foostring{"foo"}
\begin{document}
% Preamble: \pgfplotsset{width=7cm,compat=1.18}
\begin{tikzpicture}
\begin{axis}[grid=major]
\addplot+ [
unbounded coords=discard,
x filter/.code={
\edef\tempa{\thisrow{type}}
\ifx\tempa\foostring
\def\pgfmathresult{}
\fi
},
] table[trim cells=true] {
x y type
1 2 "foo"
1.5 2.5 "bar"
2 3 "foo"
2.5 3.5 "bar"
3 4 "foo"
4 5 "bar"
};
\end{axis}
\end{tikzpicture}
\end{document}