![x 値に従ってポイントをフィルターしようとすると x フィルターに問題が発生する](https://rvso.com/image/254703/x%20%E5%80%A4%E3%81%AB%E5%BE%93%E3%81%A3%E3%81%A6%E3%83%9D%E3%82%A4%E3%83%B3%E3%83%88%E3%82%92%E3%83%95%E3%82%A3%E3%83%AB%E3%82%BF%E3%83%BC%E3%81%97%E3%82%88%E3%81%86%E3%81%A8%E3%81%99%E3%82%8B%E3%81%A8%20x%20%E3%83%95%E3%82%A3%E3%83%AB%E3%82%BF%E3%83%BC%E3%81%AB%E5%95%8F%E9%A1%8C%E3%81%8C%E7%99%BA%E7%94%9F%E3%81%99%E3%82%8B.png)
x 値に従ってポイントをフィルター処理しようとしています。データは、表形式のテキスト ファイルから取得することも、座標{...}に直接入力することもできます。x filter/.code を使用しようとしましたが、テキスト ファイルのデータの場合は問題ないようです。一方、座標{...}を介してデータを直接入力すると、同じコードでコンパイル エラーが発生します。
\addplot[scatter, only marks] table[x=xx, y=yy, col sep=comma]{tmp.txt};
% Seems working
対
\addplot[scatter, only marks] coordinates{(0,0) (1,1) (1,1.5) (2,2)};
%|16 error| Missing = inserted for \ifnum. Y ...s] coordinates{(0,0) (1,1) (1,1.5) (2,2)};
x フィルターでの \pgfmathresult の使い方に問題があるのではないかと考えていますが、修正方法がわかりません。以下は、コンパイル エラーと MWE です。ファイル tmp.txt が次のとおりであると仮定します。
xx, yy
0,0
1,1
1,1.5
2,2
コードは次のとおりです。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=0.8]
\begin{axis}[
x filter/.code= {
\ifnum\pgfmathresult=1
\def\pgfmathresult{}
\fi
}]
% This seems working fine
\addplot[scatter, only marks] table[x=xx, y=yy, col sep=comma]{tmp.txt};
% This cause compilation error, which is
% |16 error| Missing = inserted for \ifnum. Y ...s] coordinates{(0,0) (1,1) (1,1.5) (2,2)};
%\addplot[scatter, only marks] coordinates{(0,0) (1,1) (1,1.5) (2,2)};
\end{axis}
\end{tikzpicture}
\end{document}
答え1
これは、座標が内部浮動小数点表現(私にとってはクリンゴン語のよう1.1Y0.e1
なもの)を介して処理されるためで、Y
エラー メッセージにそのように表示されます。解決策は、通常の 10 進数に変換することです。
整数でのみ動作しますが、pgfmath は整数でも\ifnum
吐き出す傾向があるため、複雑さを解消するには次元の比較を行う方がよいでしょう。1.0
1
\documentclass{article}
\usepackage{pgfplots}
\pgfplotstableread{
xx yy
0 0
1 1
1 1.5
2 2
}\mytable
\begin{document}
\begin{tikzpicture}[scale=0.8]
\begin{axis}
\addplot[scatter, only marks, x filter/.code= {
\ifnum\pgfmathresult=1
\def\pgfmathresult{}
\fi
}] table[x=xx, y=yy]{\mytable};
\addplot+[no marks, x filter/.code={
\pgfkeys{/pgf/fpu,/pgf/number format/.cd,fixed,verbatim}
\pgfmathprintnumberto{\pgfmathresult}{\mytempvar}
\ifdim\mytempvar pt=1pt%
\def\pgfmathresult{}
\fi
}] coordinates{(0,0) (1,1) (1,1.5) (2,2)};
\end{axis}
\end{tikzpicture}
\end{document}