0

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:

My result

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}

Daniel Duque
  • 209
  • 1
  • 12
  • You could do so by combining axis cs and rel axis cs in 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:28
  • @StefanPinnow I added a bit more detail of what my problem actually is. I can move the top coordinate by hand, but it fails in the loop. I don't understand much of the details, but I think the axis cs is not the problem here. – Daniel Duque Nov 25 '21 at 20:43
  • Use \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
  • @ABC That didn't work for me. Lines are not vertical. – Daniel Duque Nov 25 '21 at 21:06
  • 1
    Because you run it in backwards compatibility mode. Add \pgfplotsset{compat=1.18} to the preamble of your document. –  Nov 25 '21 at 21:08

0 Answers0