1

How do I add a vertical line between the bars in a histogram?

The following constructs my diagram:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[ybar stacked, symbolic x coords={1,2,3,4,5},
    xtick=data, ymin=0, bar width=30pt]
  \addplot[ybar,fill=lightgray] coordinates {
    (1,3) (2,2) (3,1) (4,5) (5,3) };
\end{axis}
\end{tikzpicture}
\end{document}

Output of this code

How do I place a vertical line between (for example), the 3rd and 4th column?

I've tried \draw (3.5,0) -- (3.5,5);, but this doesn't work. It looks like getting hold of specific points in the diagram is complicated (something to do with axis cs?) but I've not managed to make any sense of any examples nor find the place in the 500-page manual.

3
  • Add \pgfplotsset{compat=1.11} to the preamble, and coordinates are interpreted as axis coordinates by default. Or do e.g. (axis cs:3.5, 0) Jan 8 at 17:44
  • I may be doing something wrong, but that doesn't work for me. I get: ! Package pgfplots Error: Sorry, the input coordinate '3.5' has not been defined with 'symbolic x coords={1,2,3,4,5}... Maybe it has been misspelled? Or did you mean something like [normalized]3.5?. Jan 8 at 17:49
  • Oops, sorry, my mistake. I didn't see the symbolic coordinates. Jan 8 at 17:57
1

A few examples:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
If you really have 1,2,... as ticklabels, you can just remove the symbolic coordinates and use axis cs:
\begin{center}
\begin{tikzpicture}
  \begin{axis}[ybar stacked, %symbolic x coords={1,2,3,4,5},
    xtick=data, ymin=0, bar width=30pt]
  \addplot[ybar,fill=lightgray] coordinates {
    (1,3) (2,2) (3,1) (4,5) (5,3) };
\draw (axis cs:3.5,0) -- (axis cs:3.5,5.5);,
\end{axis}
\end{tikzpicture}
\end{center}

Or add compat=11 (or higher) and don't use axis cs:
\begin{center}
\pgfplotsset{compat=1.11} % normally add this to preamble
\begin{tikzpicture}
  \begin{axis}[ybar stacked, %symbolic x coords={1,2,3,4,5},
    xtick=data, ymin=0, bar width=30pt]
  \addplot[ybar,fill=lightgray] coordinates {
    (1,3) (2,2) (3,1) (4,5) (5,3) };
\draw (3.5,0) -- (3.5,5.5);,
\end{axis}
\end{tikzpicture}
\end{center}

If you have non-numeric ticklabels, use xticklabels instead of symbolic coordinates:
\begin{center}
\begin{tikzpicture}
  \begin{axis}[ybar stacked, xticklabels={foo,bar,baz,baa,abc},
    xtick=data, ymin=0, bar width=30pt]
  \addplot[ybar,fill=lightgray] coordinates {
    (1,3) (2,2) (3,1) (4,5) (5,3) };
\draw (axis cs:3.5,0) -- (axis cs:3.5,5.5);,
\end{axis}
\end{tikzpicture}
\end{center}

Instead of manually specifying a y-coordinate you can access the value used in the axis with pgfkeysvalueof:
\begin{center}
\begin{tikzpicture}
  \begin{axis}[ybar stacked, xticklabels={foo,bar,baz,baa,abc},
    xtick=data, ymin=0, bar width=30pt]
  \addplot[ybar,fill=lightgray] coordinates {
    (1,3) (2,2) (3,1) (4,5) (5,3) };
\draw (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:3.5,\pgfkeysvalueof{/pgfplots/ymax});,
\end{axis}
\end{tikzpicture}
\end{center}


\end{document}
2
  • (Not the best answer, sorry, dinnertime.) Jan 8 at 18:03
  • Thanks - that's good enough to produce what I need. Jan 8 at 18:08

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.