pgfplots 帶有浮動內容和條件的過濾表

pgfplots 帶有浮動內容和條件的過濾表

我想過濾一些數據來繪製圖,到目前為止我發現的最好的東西是這裡。

這是上述問題的相關程式碼:

  \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.的行。\addplot我認為它可能與 Tex 和 pgfplots 如何評估表達式有關,例如\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}

顯示上述程式碼結果的圖像

相關內容