# Pgfplots: xbar issues in histogram

I want to create an histogram with grouped bars of the same color and around evenly spaced (arbitrary) y-values. I managed to get this:

Now, I want to put custom ytick labels in the center of each group, but I just do not understand how PGFPlots computes these values and I do not want to tinker with coordinates by trial-and-error.

Ideas?

======

MWE:

 \documentclass{beamer}
\usepackage{pgfplots}

\begin{document}
\begin{frame}{Title}

How to put an ylabel in the exact center of each group of bars? (For that example, that would give coords like (0,55), (0,25), (0,-10) and (0,-50).)

\begin{center}
\begin{tikzpicture}
\begin{axis}[
enlarge y limits=4,
xbar,
xmin=0,
bar width=7pt,
]
\end{axis}
\end{tikzpicture}
\end{center}

\end{frame}
\end{document}

• Any special reason for your preference on Torbjorn's answer instead of mine? Dec 14 '17 at 9:57
• @Cragfelt I'm not the OP, but you appear to have done exactly what Bibi said he/she wanted to avoid: tinker with coordinates by trial and error. Dec 14 '17 at 11:59
• @Cragfelt: Torbjorn is right, for your answer you need to specify the ytick coordinates explicitly with trial and error.
– Bibi
Dec 15 '17 at 12:14

If you add xbar to the \addplot options instead of the axis options, the bars are placed at the given y-values. So you can use one \addplot per group, with different y-values. There is no need to guess the y-values, as you choose the middle one for the group. E.g. with bars at y = 0,1,2, place the ytick at y = 1.

 \documentclass{beamer}
\usepackage{pgfplots}

\begin{document}
\begin{frame}{Title}

How to put an ylabel in the exact center of each group of bars? (For that example, that would give coords like (0,55), (0,25), (0,-10) and (0,-50).)

\begin{center}
\begin{tikzpicture}
\begin{axis}[
enlarge y limits=0.1,
xmin=0,
bar width=5pt,
ytick={1,6,11,15.5},
yticklabels={foo,bar,baz,foobar},
tickwidth=0pt
]
\end{axis}
\end{tikzpicture}
\end{center}

\end{frame}
\end{document}


Something like this?

\documentclass{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{frame}{Title}

How to put an ylabel in the exact center of each group of bars? (For that example, that would give coords like (0,55), (0,25), (0,-10) and (0,-50).)

\begin{center}
\begin{tikzpicture}
\begin{axis}[
enlarge y limits=4,
xbar,
xmin=0,
bar width=7pt,
ytick=\empty,
extra y ticks={-45,-10,25,56},
extra y tick labels={$-50$, $-10$, $25$, $55$},
]