2

How can I tell pgfplots to only put a mark on the (first and) last data point of a scatter/mesh plot? I can use mark indices to select the first, but how to select the last point without knowing a priori, how many points there will be?

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.16,
}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot [
                mesh,
                mark=o,
                mark indices={1},
                mark size=1.5pt,
                mark options={draw=red},
                point meta={\thisrow{n}}
            ] table {trace.dat};
        \end{axis}
    \end{tikzpicture}
\end{document}

The file trace.dat looks like this, but much longer:

x y n r
3 -1 17 27
1 -2 18 24
-1 -3 19 45
1
\documentclass{standalone}
\usepackage{filecontents}
\begin{filecontents*}{trace.dat}
x y n r
3 -1 17 27
1 -2 18 24
-1 -3 19 45
\end{filecontents*}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.16,
}

\begin{document}
\pgfplotstableread{trace.dat}\datatable   
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\rownum}{\pgfplotsretval}

    \begin{tikzpicture}
        \begin{axis}
            \addplot [
                mesh,
                mark=o,
                mark indices={1,\rownum},
                mark size=1.5pt,
                mark options={draw=red},
                point meta={\thisrow{n}}
            ] table {trace.dat};
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.