2

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:

enter image description here

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,
    ]
       \addplot[fill=blue!20] coordinates { (0.387,0) };
       \addplot[fill=blue!20] coordinates { (0.199,0) };
       \addplot[fill=blue!20] coordinates { (0.192,0) };
       \addplot[fill=green!20] coordinates { (0.145,5) };
       \addplot[fill=green!20] coordinates { (0.142,5) };
       \addplot[fill=green!20] coordinates { (0.137,5) };
       \addplot[fill=green!20] coordinates { (0.132,5) };
       \addplot[fill=green!20] coordinates { (0.131,5) };
       \addplot[fill=red!20] coordinates { (0.104,10) };
       \addplot[fill=red!20] coordinates { (0.103,10) };
       \addplot[fill=red!20] coordinates { (0.098,10) };
       \addplot[fill=orange!20] coordinates { (0.143,15) };
       \addplot[fill=orange!20] coordinates { (0.141,15) };
       \addplot[fill=orange!20] coordinates { (0.137,15) };
       \addplot[fill=orange!20] coordinates { (0.120,15) };
       \end{axis}
    \end{tikzpicture}
 \end{center}

 \end{frame}
 \end{document}
3
  • Any special reason for your preference on Torbjorn's answer instead of mine?
    – Cragfelt
    Commented Dec 14, 2017 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. Commented Dec 14, 2017 at 11:59
  • @Cragfelt: Torbjorn is right, for your answer you need to specify the ytick coordinates explicitly with trial and error.
    – Bibi
    Commented Dec 15, 2017 at 12:14

2 Answers 2

0

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.

output

 \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
    ]
       \addplot[xbar,fill=blue!20] coordinates { (0.387,0)(0.199,1)(0.192,2) };
       \addplot[xbar,fill=green!20] coordinates { (0.145,4)(0.142,5)(0.137,6)(0.132,7)(0.131,8) };
       \addplot[xbar,fill=red!20] coordinates { (0.104,10)(0.103,11)(0.098,12) };
       \addplot[xbar,fill=orange!20] coordinates { (0.143,14)(0.141,15)(0.137,16)(0.120,17) };
       \end{axis}
    \end{tikzpicture}
 \end{center}

 \end{frame}
 \end{document}
0

Something like this?

enter image description here

\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$},
    ]
       \addplot[fill=blue!20] coordinates { (0.387,0) };
       \addplot[fill=blue!20] coordinates { (0.199,0) };
       \addplot[fill=blue!20] coordinates { (0.192,0) };
       \addplot[fill=green!20] coordinates { (0.145,5) };
       \addplot[fill=green!20] coordinates { (0.142,5) };
       \addplot[fill=green!20] coordinates { (0.137,5) };
       \addplot[fill=green!20] coordinates { (0.132,5) };
       \addplot[fill=green!20] coordinates { (0.131,5) };
       \addplot[fill=red!20] coordinates { (0.104,10) };
       \addplot[fill=red!20] coordinates { (0.103,10) };
       \addplot[fill=red!20] coordinates { (0.098,10) };
       \addplot[fill=orange!20] coordinates { (0.143,15) };
       \addplot[fill=orange!20] coordinates { (0.141,15) };
       \addplot[fill=orange!20] coordinates { (0.137,15) };
       \addplot[fill=orange!20] coordinates { (0.120,15) };
       \end{axis}
    \end{tikzpicture}
 \end{center}

 \end{frame}
 \end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .