0

I've got a table with a binary column named xrayOn. Instead of plotting this, I want to write each xrayOn value (i.e. 0 or 1) at the position (index, 1) in a node. However, in the code below only the addplot command works. The for loop results in the errors:

  • Undefined control sequence.
  • Argument of \pgfmathfloatparse@@ has an extra }.
  • Runaway argument?
  • Extra }, or forgotten \endgroup.
  • llegal unit of measure (pt inserted).
  • Missing number, treated as zero.

What do I miss here? Further, I would appreciate a short explanation of when to use foreach, pgfplotsinvokeforeach or edef in the pgfplots axis enivorment.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning,chains,decorations.text,arrows.meta,patterns,patterns.meta,calc}
\usepackage{pgfplots}
\usepackage{pdfpages}
\usepackage{pgfplotstable}
\usepackage{ifthen}

\pgfplotsset{compat=1.18}

\pgfplotstableread[col sep=comma]{figures/model/mytable.csv}\loadedtable

\begin{document}

\begin{tikzpicture}
\begin{axis}
    \addplot [restrict x to domain=100:500] table [x expr={\thisrow{index}}, y expr={\thisrow{xrayOn}}, ] {\loadedtable};
    
    \foreach \i in {100,101,...,500} {
        \pgfplotstablegetelem{\i}{xrayOn}\of\loadedtable
        \let\indexvalue\pgfplotsretval
        \node at (axis cs: \indexvalue,1) [anchor=south] {\indexvalue}; % Position text nodes at relevant coordinates
    }
\end{axis}
\end{tikzpicture}
\end{document}
4
  • Does this answer your question? \foreach not behaving in axis environment Commented Jul 12, 2024 at 13:34
  • Yes, it does. They referred to the manual and on the recommended page I found the loop-in-pgfplots explanation and spotted the nodes near coords option. With that, I can eliminate the for loop issue and it is much more convenient. Plot look nice now! :) Thanks for the quick response and have a nice weekend. :) Cheers! Commented Jul 12, 2024 at 15:01
  • Please always complete your MWE. tex.meta.stackexchange.com/questions/228/… Commented Jul 12, 2024 at 15:08
  • Please note: The ``` marks for code has to be a line on their own to mark code blocks. See the markdown reference manual for more information. I've corrected this in your posting, so you can see the difference. Commented Jul 12, 2024 at 15:30

1 Answer 1

0
\begin{filecontents*}{mytable.csv}
index, xrayOn
0,0
11,11
22,22
33,33
\end{filecontents*}
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotstableread[col sep=comma]{mytable.csv}\loadedtable
\addplot table [x expr={\thisrow{index}}, y expr={\thisrow{xrayOn}}, ] {\loadedtable};
\pgfplotsinvokeforeach {0,1,...,3} {
\path \pgfextra{\pgfplotstablegetelem{#1}{xrayOn}\of\loadedtable} (\pgfplotsretval,1) node[anchor=south]{\pgfplotsretval};
}
\end{axis}
\end{tikzpicture}
\end{document}

Linear graph with some nodes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.