I am trying to add vertical lines to my plot. The problem is that I don't know what ymax is, so I cannot just draw them easily. So far I have been able to:
But I don't know how to shift the top position in a similar way to the bottom.
I can move the top position manually as e.g:
\addplot[draw=gray] (16*\i-0.5,0) --
($ (current axis.above origin)+(15.5,0) $);
But it fails when I do it with the variable \i as:
\addplot[draw=gray] (16*\i-0.5,0) --
($ (current axis.above origin)+(16*\i-0.5,0) $);
I get the error:
! Undefined control sequence.
\pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@
l.38 \end{axis}
?
)
Runaway argument?
\def \expandafter \pgfmath@number \expandafter {\the \pgfmath@count }\ETC.
! File ended while scanning use of \pgfmath@dimen@@.
<inserted text>
\par
<*>
Furthermore, the lines don't go all the way to the top, they only go up until the ticks. How can I make the lines go all the way to the top (or move the ticks to match the lines only in the top)?
\documentclass[11pt]{article}
\usepackage[top=2.54cm,bottom=2.54cm,left=2.75cm,right=2.75cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\begin{filecontents}{data.txt}
0 10000
1 15000
2 10000
3 20000
4 5000
5 0
\end{filecontents}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis} [
height = \axisdefaultheight,
width = \textwidth,
xlabel = Channel,
ylabel = Counts,
ymin = 0,
xmin = -5,
xmax = 132,
axis on top,
]
% Vertical division of each Board [0-7]
\foreach \i in {0,...,8} {
\node (a) at (current axis.above origin) {};
\node (b) at ($ (a) + (0-0.5,0) $) {};
\addplot[draw=gray] (16*\i-0.5,0) -- (b);
}
\addplot[
ybar interval,
x filter/.code=\pgfmathparse{#1-0.5},
fill = black!20] table {data.txt};
\end{axis}
\end{tikzpicture}
\caption{Plot.}
\end{figure}
\end{document}

axis csandrel axis csin one coordinate, similar as I have shown in https://tex.stackexchange.com/a/622134/95441. Thus, I consider this being a duplicate. Nevertheless you could state per comment if this solved your problem. If not, please also let us know in a comment or by editing your question. – Stefan Pinnow Nov 25 '21 at 20:28axis csis not the problem here. – Daniel Duque Nov 25 '21 at 20:43\path (current axis.above origin) coordinate (T); \pgfplotsinvokeforeach{0,...,8}{ \draw[draw=gray] (16*#1-0.5,0) -- (16*#1-0.5,0|-T); }. – Nov 25 '21 at 20:58\pgfplotsset{compat=1.18}to the preamble of your document. – Nov 25 '21 at 21:08