2

How to show the entire bars and slightly extend the y axis in both directions (top and bottom)?

\documentclass{standalone}
   \usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xbar,
            xmin = 0,
            y = 35pt,
            bar width= 2em,
            axis x line       = none,
            axis y line       = left,
            tickwidth         = 0pt,
            symbolic y coords = {A,B,C,D},
            nodes near coords,
          ]
          \addplot coordinates { (45,A) (55,B) (40,C) (50,D) };
         \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

1

Yes. You can enlarge the y limits with enlarge y limits.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xbar,
            xmin = 0,
            y = 35pt,
            bar width= 2em,
            axis x line       = none,
            axis y line       = left,
            tickwidth         = 0pt,
            symbolic y coords = {A,B,C,D},
            nodes near coords,enlarge y limits=0.2
          ]
          \addplot coordinates { (45,A) (55,B) (40,C) (50,D) };
         \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • Do you know why if enlarge y limits=0.2, is inserted before, for example between xmin = 0 and y = 35pt, this key doesn't have the expected effect? I don't find in the manual a such restriction. – quark67 Mar 17 at 4:27
  • @quark67 On p. 329 it is written that "Specifying a number value like ‘enlarge x limits=0.2’ will enlarge lower and upper axis limit rela- tively. The following example adds 20% of the axis limits on both sides:" and I think the analogous statement applies to enlarge y limits. – user121799 Mar 17 at 4:57
  • I speak about the position of the key enlarge y limits. You add it after nodes near coords. But if you move it between for example, xmin = 0 and y = 35pt, it seems don’t have any effect (ie: it gave the same output as the OP screenshot). Is this behavior only on my installation? – quark67 Mar 17 at 5:10
  • 1
    Thank marmot. I tried this before, but as @quark67 said I was placing it just after y = 35pt and it was not working. – jak123 Mar 17 at 13:47

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.