3

I'm trying to visualize some data with bar graphs. Some of those bar graphs work fine and some (with the same code, just different data points) have the bars "floating above the x-axis". This bar graph works fine:

\begin{tikzpicture}
        \begin{axis}[
            ybar,
            bar width=40,
            enlarge x limits=0.3,
            legend style={at={(0.5,-0.2)},
            anchor=north,legend columns=-1},
            ylabel={\# Participants},
            symbolic x coords={no limit,unclear},
            xtick=data,
            nodes near coords,
            ]
            \addplot coordinates {(no limit,17) (unclear,5)};
        \end{axis}
    \end{tikzpicture}

And this one has a distance between the onset of the bars and the x-axis that I would like to eliminate:

\begin{tikzpicture}
        \begin{axis}[
            ybar,
            bar width=40,
            enlarge x limits=0.3,
            legend style={at={(0.5,-0.2)},
            anchor=north,legend columns=-1},
            ylabel={\# Participants},
            symbolic x coords={x-10,other},
            xtick=data,
            nodes near coords,
            ]
            \addplot coordinates {(x-10,22) (other,0)};
        \end{axis}
    \end{tikzpicture}

I found different similar questions on tex-exchange, but the solutions don't seem to apply: I don't have an "enlarge y limits"-factor, and one solution mentioned setting ymin=0 as an argument in axis, which didn't change anything.

  • 1
    add enlarge y limits={upper=0}, to the second one – percusse Aug 21 '16 at 10:52
  • Thank you very much! It works for this example, but for this one: symbolic x coords={ff,fff,unclear}, \addplot coordinates {(ff,1) (fff,1) (unclear,20)}; it eats the bars of the two data points that should be 1 high, and they are now 0 high... – Kora K Aug 21 '16 at 10:55
  • That is, if in this example the following coordinates should be plotted: {(x-10,22) (other,1)}, the bar for other would be 0 high. It should be 1 high. How do I solve this problem? – Kora K Aug 21 '16 at 15:04
2

While @percusse has given you a working solution for graph 2, ymin=0 also works, but I note you said it didn't work for you. There must be a difference between what I have posted below and what you tested. You can also use ymin=0 in the second graph, if I have interpreted the problem correctly.

enter image description here

This is the code, only very slightly modified from your version by adding ymin=0,. By the way, it is always best to post full, compilable minimum working examples, inclusive of document class, the minimum packages and libraries that you are using.

\documentclass[border=10pt,crop=true]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    ybar,
    bar width=40,
    ymin=0, %<--- Set the y-axis minimum value
    enlarge x limits=0.3,
    legend style={at={(0.5,-0.2)},
        anchor=north,legend columns=-1},
    ylabel={\# Participants},
    symbolic x coords={x-10,other},
%    enlarge y limits={upper=0}, % another option to reset the x-axis intersection with the y-axis for this graph
    xtick=data,
    nodes near coords,
    ]
    \addplot coordinates {(x-10,22) (other,0)};
    \end{axis}
    \end{tikzpicture}%
    \qquad
    \begin{tikzpicture}
    \begin{axis}[
    ybar,
    bar width=40,
    ymin=0, %<--- Set the y-axis minimum value
    enlarge x limits=0.3,
    legend style={at={(0.5,-0.2)},
        anchor=north,legend columns=-1},
    ylabel={\# Participants},
    symbolic x coords={x-10,other},
    xtick=data,
    nodes near coords,
    ]
    \addplot coordinates {(x-10,22) (other,1)};
    \end{axis}
    \end{tikzpicture}
\end{document}
  • 1
    Thanks a lot! So simple and it works :) I will specify the included packages next time, that absolutely makes sense. – Kora K Aug 21 '16 at 21:18

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.