丢弃 pgfplots 中的值 = 0

丢弃 pgfplots 中的值 = 0

在下面的代码中,我该如何删除 y1 的所有条目y2 值 = 0?谢谢您的帮助。

以下是由Stefan Pinnow 发现于此处

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
    \begin{filecontents*}{data.csv}
        Iter,   y1,         y2
         1,     -0.9317521, -1.5
         2,     1.8946202,  1.8946202
         3,     0,          0
         4,     0,  1.5797030
         5,     -1.8814457, -1.8814457
         6,     0,          0
         7,     2.0373926,  0
         8,     0,          0
         9,     1.9972528,  1.9972528
        10,     0,          0
    \end{filecontents*}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=11,
            xlabel=Number~of~Recursions,
            ylabel=Absolute Parameter Error,
        ]
            \addplot [
                mark=*,
                only marks,
                mark options={
                    %%% only draw the marker
                    % draw=color, fill=none
                    draw=red,
                    fill=red,
                    % adjust the line width of the marker
                    line width=1pt,
                }
            ] table [x=Iter,y=y2,col sep=comma] {data.csv};
            \addplot [
                ycomb,
                mark=*,
                blue,
                fill=blue,
                line width=1pt,
                % make marker smaller
                mark options={
                    scale=.5,
                }
            ] table [x=Iter,y=y1,col sep=comma] {data.csv};
            \addplot [
                blue,
                sharp plot,
                line width=1pt,
            ] coordinates {
                (\pgfkeysvalueof{/pgfplots/xmin},0)
                (\pgfkeysvalueof{/pgfplots/xmax},0)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

答案1

您可以使用一个restrict expr to domain键来实现这一点。如果您设置了restrict expr to domain={\thisrow{y1}==0 && \thisrow{y2}==0}{0:0},那么只有表达式y1==0 and y2==1为假的点(即表达式计算为0,位于域内0:0)才会被保留:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
    \begin{filecontents*}{data.csv}
        Iter,   y1,         y2
         1,     -0.9317521, -1.5
         2,     1.8946202,  1.8946202
         3,     0,          0
         4,     0,  1.5797030
         5,     -1.8814457, -1.8814457
         6,     0,          0
         7,     2.0373926,  0
         8,     0,          0
         9,     1.9972528,  1.9972528
        10,     0,          0
    \end{filecontents*}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=0,
            xmax=11,
            xlabel=Number~of~Recursions,
            ylabel=Absolute Parameter Error
        ]
            \addplot [
                mark=*,
                only marks,
                mark options={
                    %%% only draw the marker
                    % draw=color, fill=none
                    draw=red,
                    fill=red,
                    % adjust the line width of the marker
                    line width=1pt,
                }
            ] table [x=Iter,y=y2,col sep=comma, restrict expr to domain={\thisrow{y1}==0 && \thisrow{y2}==0}{0:0}] {data.csv};
            \addplot [
                ycomb,
                mark=*,
                blue,
                fill=blue,
                line width=1pt,
                % make marker smaller
                mark options={
                    scale=.5,
                }
            ] table [x=Iter,y=y1,col sep=comma, restrict expr to domain={\thisrow{y1}==0 && \thisrow{y2}==0}{0:0}] {data.csv};
            \addplot [
                blue,
                sharp plot,
                line width=1pt,
            ] coordinates {
                (\pgfkeysvalueof{/pgfplots/xmin},0)
                (\pgfkeysvalueof{/pgfplots/xmax},0)
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

相关内容