25

I'm trying to construct a grouped bar chart and have three questions I can't seem to find the answer to:

  1. How do I modify the spacings between the groups? Or at least make sure that no bars are outside the chart?

  2. How do I remove the spacing between bars in a group?

  3. How do I in the legend add a single space between each symbol and description?

\documentclass[12pt,a4paper,onecolumn, openright]{report}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}

% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.85*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Run time speed},
        symbolic x coords={EgyptHD,Hover,Navi},
        xtick = data,
        scaled y ticks = false,
    ]
        \addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {(EgyptHD, 1.0) (Hover,1.0) (Navi,1.0)};

        \addplot[style={rred,fill=rred,mark=none}]
            coordinates {(EgyptHD,1.123) (Hover,0.85) (Navi,1.09)};

        \addplot[style={ggreen,fill=ggreen,mark=none}]
            coordinates {(EgyptHD,0.92) (Hover,0.56) (Navi,0.95)};

        \addplot[style={ppurple,fill=ppurple,mark=none}]
            coordinates {(EgyptHD,0.74) (Hover,1.07) (Navi,1.23)};

        \legend{No vectorization,TreeScore $>2$,TreeScore $>3$,TreeScore $>4$}
    \end{axis}
\end{tikzpicture}
\end{document}

output

1 Answer 1

37
  1. To decrease the space between the groups, you can increase the x axis range by using enlarge x limits=0.25 (which increases the axis range by 25% compared to the data range).
  2. To remove the spacing between the bars, use ybar=0pt instead of just ybar. Note that this will lead to slight overlaps between the bars due to their outlines. To avoid this, you could either set ybar=2*\pgflinewidth, or disable drawing the bar outlines by setting draw opacity=0 in the plot options.
  3. To get a fixed space between the legend images and labels, you can align the labels on the left and set the space between the columns using legend cell align=left, legend style={ column sep=1ex } (thanks, Qrrbrbirlbel!).

\documentclass[12pt,a4paper,onecolumn, openright]{report}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{tikz}

% Define bar chart colors
%
\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}
\definecolor{ppurple}{HTML}{9F4C7C}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = 0.85*\textwidth,
        height = 8cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Run time speed},
        symbolic x coords={EgyptHD,Hover,Navi},
        xtick = data,
        scaled y ticks = false,
        enlarge x limits=0.25,
        ymin=0,
        legend cell align=left,
        legend style={
                at={(1,1.05)},
                anchor=south east,
                column sep=1ex
        }
    ]
        \addplot[style={bblue,fill=bblue,mark=none}]
            coordinates {(EgyptHD, 1.0) (Hover,1.0) (Navi,1.0)};

        \addplot[style={rred,fill=rred,mark=none}]
             coordinates {(EgyptHD,1.123) (Hover,0.85) (Navi,1.09)};

        \addplot[style={ggreen,fill=ggreen,mark=none}]
             coordinates {(EgyptHD,0.92) (Hover,0.56) (Navi,0.95)};

        \addplot[style={ppurple,fill=ppurple,mark=none}]
             coordinates {(EgyptHD,0.74) (Hover,1.07) (Navi,1.23)};

        \legend{No vectorization,TreeScore $>2$,TreeScore $>3$,TreeScore $>4$}
    \end{axis}
\end{tikzpicture}
\end{document}
2
  • 1
    Yeah, we had that with the legend a few question before. Interestingly, style={draw=none} does have an affect on the legend. It does reactivate the outlines in my solution without draw=none. Very confusing. Commented Mar 7, 2013 at 16:03
  • @Jake Sorry to bring up such an old thread, but is it possible to have two y-axis? Where one group of data (which the x-axis label would call "Group 1") uses the left axis and a second group of data (where the x-axis label would call "Group 2") uses the right axis? They would use the exact same legend and share a single x-axis.
    – Kevin Dong
    Commented Aug 23, 2013 at 1:09

You must log in to answer this question.

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