Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I increase the spacing between the bars in a bar plot from pgfplots?

\documentclass{minimal}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    every axis plot post/.style={/pgf/number format/fixed},
    ybar,
    x=3cm,
    ymin=-0.1,
    ymax=12,
    xtick=data,
    enlarge x limits=0.2,
    bar width=15pt,
    symbolic x coords={A,B,C},
    nodes near coords,
    axis lines*=left,
    ]
\addplot coordinates {(A,2) (B,0.5) (C,10)};
\addplot coordinates {(A,20) (B,30) (C,1)};
\addplot coordinates {(A,6) (B,11) (C,7)};
\end{axis}
\end{tikzpicture}
\end{document}

I want to increase the space between the bars (A,2), (A,20), (A,6) (and analogously with B's and C's).

And on another note: How can I deal with values that are too large to be displayed? (e.g (B,30))

share|improve this question
    
Welcome to TeX.SX. –  Marc van Dongen Jan 1 '13 at 23:02

1 Answer 1

up vote 11 down vote accepted

You can set the spacing between the bars using the optional argument to ybar (the default is 2pt).

For indicating that some of the data values exceed the axis range, you could clip the values at some level that's slightly larger than your axis limit using restrict y to domain*=0:<value>, making sure that you set clip=false so that the bars can protrude beyond the axis. You can make the original unclipped y values available for use in the nodes near coords by setting visualization depends on=rawy \as \rawy, and draw a line indicating the interruption using a after end axis/.code key:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    every axis plot post/.style={/pgf/number format/fixed},
    ybar=5pt,
    bar width=12pt,
    x=3cm,
    ymin=0,
    axis on top,
    ymax=12,
    xtick=data,
    enlarge x limits=0.2,
    symbolic x coords={A,B,C},
    restrict y to domain*=0:14, % Cut values off at 14
    visualization depends on=rawy\as\rawy, % Save the unclipped values
    after end axis/.code={ % Draw line indicating break
            \draw [ultra thick, white, decoration={snake, amplitude=1pt}, decorate] (rel axis cs:0,1.05) -- (rel axis cs:1,1.05);
        },
    nodes near coords={%
            \pgfmathprintnumber{\rawy}% Print unclipped values
        },
    axis lines*=left,
    clip=false
    ]
\addplot coordinates {(A,2) (B,0.5) (C,10)};
\addplot coordinates {(A,20) (B,30) (C,1)};
\addplot coordinates {(A,6) (B,11) (C,7)};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Thanks for your quick answer. Do you know how to deal with the large bars (A,20), (B,30)? I don't want to set the ymax to a higher value, but I want convey that these bars are too high. –  MartinG Jan 1 '13 at 23:35
    
@MartinG: I've edited my answer to include one way of interrupting the bars. Is that what you were thinking of? –  Jake Jan 1 '13 at 23:57
    
Yes, thanks, that's what I meant. –  MartinG Jan 2 '13 at 0:29
    
@Jake The problem with this solution is that it only works if all the data groups have values in range. If you remove your first and third \addplot statements, plotting just {(A,20) (B,30) (C,1)} then this breaks. Any suggestions on how to fix that? –  Nicolas Wu Apr 4 '13 at 13:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.