PGFPlots Data Points – Remove Data Points Without Changing Original Table

pgfplotstikz-pgf

I would like to know a way to remove one or more data points from a plot, using pgfplots, generated by an external table "data.csv". In the minimal example below, I would like to remove the third point (3,5) without changing the original table.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
a,b,c,d
1,4,5,1
2,3,1,5
3,5,6,1
4,1,4,9
5,3,4,7
\end{filecontents*}


\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [x=a, y=c, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}

Edit

Also, it would be nice to somehow tell pgfplots to erase specifically the "third point", without telling the coordinates. Or at least, only providing the abcissa value. A similar feature is encountered in Origin software, where you choose the points to erase by hand.

Best Answer

You can easily do what you want. Just use something like the following:

\begin{tikzpicture}
    \begin{axis}[skip coords between index={2}{3}]
        \addplot table [x=a, y=c, col sep=comma] {data.csv};
    \end{axis} 
\end{tikzpicture}

Notice that when you want to remove line 3, it is the first number (2) that identifies it, because if you have ...index={a}{b} it removes lines with index i such that a<= i<b. This is true because we start counting at i=0, so the line you want to remove is indexed by i=2. You can add multiple lines to be skipped if you want, like:

...
\begin{axis}[skip coords between index={2}{3},
             skip coords between index={5}{6}]
...

This will remove data line 3 (i=2) and 6 (i=5)in the original file (not counting headers, and assuming you had that many lines). In your example, now with data in a seperate file, you could remove the first (0) and last (4) using:

\documentclass{article}
\usepackage{pgfplots}

    \begin{document}
    \begin{tikzpicture}
            \begin{axis}[skip coords between index={0}{1},
                         skip coords between index={4}{5}]
                \addplot table [x=a, y=c, col sep=comma] {data.csv};
            \end{axis}
    \end{tikzpicture}
\end{document}

Producing

enter image description here