
プロット用にいくつかのデータをフィルタリングしたいのですが、これまでのところ私が見つけた最良の方法はこれはここです。
これは、前述の質問に関連するコードです。
\pgfplotsset{
discard if not and smaller/.style n args={4}{
x filter/.code={
\edef\tempa{\thisrow{#1}}
\edef\tempb{#2}
\edef\tempc{\thisrow{#3}}
\edef\tempd{#4}
\ifnum\tempa=\tempb
\ifnum\tempc<\tempd
\def\pgfmathresult{inf}
\else
\fi
\else
\def\pgfmathresult{inf}
\fi
}
}
}
\begin{tikzpicture}
\begin{axis}
\addplot [ultra thick,
black,
discard if not and smaller={P}{0}{X}{5}] table [x=X, y=Y] {data.dat};
\end{tikzpicture}
私の知る限り、条件文は\ifnum
整数でのみ機能します。そのため、浮動小数点数が必要なので、これを次元に置き換えようとしました。また、不等式をチェックしたい(つまり、データは特定の範囲内にある必要があります)
\pgfplotsset{
discard if out of range/.style n args={3}{
x filter/.code={
\edef\tempa{\thisrow{#1}}
\edef\tempb{#2}
\edef\tempc{#3}
\ifdim\tempa pt> \tempb pt
\ifdim\tempa pt< \tempc pt
\else
\def\pgfmathresult{inf}
\fi
\else
\def\pgfmathresult{inf}
\fi
}
}
}
しかし、新しいオプションを利用するMissing number, treated as zero.
行で常にエラーが発生します。これは、Tex と pgfplots が式を評価する方法と関係があるのではないかと思いますが、これをどうやって調べればよいのかわかりません...\addplot
\thisrow{#1}
最後に、完全な例を示します。
\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable,filecontents}
\begin{filecontents}{file.dat}
x y z
0 1 2
2 3 3.5
3 4 5
\end{filecontents}
\pgfplotsset{
discard if out of range/.style n args={3}{
x filter/.code={
\edef\tempa{\thisrow{#1}}
\edef\tempb{#2}
\edef\tempc{#3}
\ifdim\tempa pt> \tempb pt
\ifdim\tempa pt< \tempc pt
\else
\def\pgfmathresult{inf}
\fi
\else
\def\pgfmathresult{inf}
\fi
}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[unbounded coords=discard ,filter discard warning=false,]
\pgfplotstableread{file.dat}\datatable
\addplot[scatter,point meta=explicit,] table [
meta = z,
discard if out of range={x}{0.5}{4},
] from \datatable {};
\end{axis}
\end{tikzpicture}
\end{document}
答え1
閉じた範囲内の値をフィルタリングしたいだけの場合は、restrict x to domain=<min>:<max>
すでに述べたようにキーを使用する必要があります。質問に対するコメント。
以下は、提供されたコードからこの機能を使用した実際の MWE です。
\begin{filecontents}{file.dat}
x y z
0 1 2
2 3 3.5
3 4 5
\end{filecontents}
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotstableread{file.dat}\datatable
% without restricting the domain
\addplot+ [very thick] table {\datatable};
% with restricting the domain
\addplot table [
restrict x to domain=0.5:4,
] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}