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
좌표는 내부 부동 소수점 표현을 통해 처리되기 때문에 Klingon과 같은 것 (또는 이와 유사한 것) 이 오류 메시지에서 1.1Y0.e1
표시됩니다 . Y
해결책은 이를 일반 십진수로 변환하는 것입니다.
또한 정수에서만 작동하지만 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}