x 値に従ってポイントをフィルターしようとすると x フィルターに問題が発生する

x 値に従ってポイントをフィルターしようとすると x フィルターに問題が発生する

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.01

\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}

ここに画像の説明を入力してください

関連情報