4

In the code below, how do I go about removing all the enteries where the y1 AND y2 value = 0? Thanks for your help.

Here is the original code that was masterfully written by Stefan Pinnow found here:

\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}
Joe
  • 9,080

1 Answers1

4

You can use a restrict expr to domain key for this. If you set restrict expr to domain={\thisrow{y1}==0 && \thisrow{y2}==0}{0:0}, then only points where the expression y1==0 and y2==1 is false (i.e. the expression evaluates to 0, which lies in the domain 0:0) are kept:

\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}
Jake
  • 232,450