0

After reading a table and plotting the data as desired, I need to add some annotations to the diagram. These annotations need to be placed manually, as they are not contained in the database export I received from my colleagues.

In the current data/table format, only data read from the first table row needs to be annotated. Trying to achieve this, I came up with two approaches:

  1. Manually place a \node at the desired coordinates (blue annotation in my MWE): Easy approach, but I was not able to use the coordinates from the table. Reading elements from the table is possible with pgfplotstable's \pgfplotstablegetelem, but I did not figure out how to use a read value for later usage.
  2. Place a node after plotting the data using \addplot. The according part of my MWE was inspired by Jake's answer, but places a node at the end of the plot, not at the start.
\documentclass{article}

\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{compat=newest}


\begin{filecontents}{sample_data.csv}
    x     y
    0.5   0
    0.5   0.7
\end{filecontents}


\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xmin = 0, xmax = 1, ymin = 0, ymax = 1]
            \pgfplotstableread{sample_data.csv}\mydata;

            \addplot+[mark=none, dashed, gray] table [
                x = x,
                y = y,
            ] {\mydata};

            \node[blue, anchor=south west] at (0.5, 0) {a};  % <-- '0.5, 0' coordinates of node (blue) should be dynamically read from table (first row)
            
            \addplot+[red, mark=none, draw=none] table[x=x, y=y] {\mydata} node[anchor=south west] {a}; % <-- places node (red) at last point of table, but should be placed at first point
        \end{axis}
    \end{tikzpicture}
\end{document}

1 Answer 1

1

Adaptations

You can move the node along the plot line by using the optional [pos=<fraction>] key.

Result

enter image description here

Code

\documentclass{article}

\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{compat=newest}

\begin{filecontents}{sample_data.csv}
    x     y
    0.5   0
    0.5   0.7
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xmin = 0, xmax = 1, ymin = 0, ymax = 1]
            \pgfplotstableread{sample_data.csv}\mydata;

            \addplot+[mark=none, dashed, gray] table [
                x = x,
                y = y,
            ] {\mydata};
            
            \addplot+[ mark=none, draw=none] table[x=x, y=y] {\mydata} node[anchor=south west, pos=0, blue] {a} node[anchor=south west, pos=1, red] {b};
        \end{axis}
    \end{tikzpicture}
\end{document}
1
  • 1
    Thanks for your help. Totally missed the pos argument -.-
    – albert
    Commented Jan 23, 2023 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .