2

I am creating a grouped bar chart with logarithmic scale but I encountered some problems. In the below picture you can see that the ticks on the x-scale are not positioned right. The beginning is correct, but "St.8" for example is too far left.

picture 1

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

\definecolor{bblue}{HTML}{4F81BD}
\definecolor{rred}{HTML}{C0504D}
\definecolor{ggreen}{HTML}{9BBB59}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        width  = 0.9\textwidth,
        height = 8cm,
        bar width=4pt,      
        major x tick style = transparent,
        symbolic x coords={St.~1,St.~2,St.~3,St.~4,St.~5,St.~6,St.~7,St.~8},
        enlarge x limits=0.1,       
        xlabel={Stufe des Algorithmus}, 
        x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
        ymin = 0,
        ybar = \pgflinewidth,   
        ymajorgrids = true,
        ylabel = {Anzahl},
        ymode=log,
        log basis y={10},
        legend cell align=left,
        legend style={
            at={(1,1.05)},
            anchor=south east,
            column sep=1ex
        }
        ]

        \addplot[style={bblue,fill=bblue,mark=none}] coordinates {(St.~1, 1) (St.~2,10006) (St.~3,99895) (St.~4, 99867)};   
        \addplot[style={rred,fill=rred,mark=none}] coordinates {(St.~1,1) (St.~2,10006) (St.~3,99448) (St.~4, 99487)};
        \addplot[style={ggreen,fill=ggreen,mark=none}] coordinates {(St.~1,1) (St.~2,10) (St.~3,10) (St.~4, 10)};

        \addplot[style={bblue!50,fill=bblue!50,mark=none}] coordinates {(St.~5,99810) (St.~6,99857) (St.~7, 80000) (St.~8, 40000)};
        \addplot[style={rred!50,fill=rred!50,mark=none}] coordinates {(St.~5,99416) (St.~6,99463) (St.~7, 79685) (St.~8, 39842)};
        \addplot[style={ggreen!50,fill=ggreen!50,mark=none}] coordinates {(St.~5,10) (St.~6,10) (St.~7, 10) (St.~8, 0)};

        \legend{Muster,Subgruppen,Gekürzt}
    \end{axis}
\end{tikzpicture}

\end{document}

I tried rearranging the code for the plots and got a better result, but the bars are not grouped anymore. What can I do to keep the grouping and correct the x-axis?

% blue
\addplot[style={bblue,fill=bblue,mark=none}] coordinates {(St.~1, 1) (St.~2,10006) (St.~3,99895) (St.~4, 99867)};   
\addplot[style={bblue!50,fill=bblue!50,mark=none}] coordinates {(St.~5,99810) (St.~6,99857) (St.~7, 80000) (St.~8, 40000)};

% red
\addplot[style={rred,fill=rred,mark=none}] coordinates {(St.~1,1) (St.~2,10006) (St.~3,99448) (St.~4, 99487)};
\addplot[style={rred!50,fill=rred!50,mark=none}] coordinates {(St.~5,99416) (St.~6,99463) (St.~7, 79685) (St.~8, 39842)};

% green
\addplot[style={ggreen,fill=ggreen,mark=none}] coordinates {(St.~1,1) (St.~2,10) (St.~3,10) (St.~4, 10)};
\addplot[style={ggreen!50,fill=ggreen!50,mark=none}] coordinates {(St.~5,10) (St.~6,10) (St.~7, 10) (St.~8, 0)};

picture 2

1 Answer 1

2

So you would like to have something like the following?

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.3}
    \definecolor{bblue}{HTML}{4F81BD}
    \definecolor{rred}{HTML}{C0504D}
    \definecolor{ggreen}{HTML}{9BBB59}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=0.9\textwidth,
        height=8cm,
        bar width=4pt,
        % commented to see, if the result is correct
%        major x tick style=transparent,
        symbolic x coords={St.~1,St.~2,St.~3,St.~4,St.~5,St.~6,St.~7,St.~8},
        enlarge x limits=0.1,
        xlabel={Stufe des Algorithmus},
        x label style={at={(axis description cs:0.5,-0.1)},anchor=north},
        % changed from `\pgflinewidth' to 1 easier see that the result is correct
        ymin=1,
        % changed to 0 to make the computation of the shift easier
        ybar=0pt,
        ymajorgrids=true,
        ylabel={Anzahl},
        ymode=log,
        legend cell align=left,
        legend style={
            at={(1,1.05)},
            anchor=south east,
            column sep=1ex
        },
    ]
        % add a scope around the to shift bars
        \begin{scope}[
            % shift the bars accordingly
            xshift={0.75*\pgfplotbarwidth},
            % and just "fill" the bars
            % (to avoid overlapping of the more right bars to the more left ones)
            draw=none,
        ]
            % your style could heavily be simplified to just providing the `+'
            % sign and the color as option
            \addplot+ [bblue]  coordinates
                {(St.~1, 1) (St.~2,10006) (St.~3,99895) (St.~4, 99867)};
            \addplot+ [rred]   coordinates
                {(St.~1,1) (St.~2,10006) (St.~3,99448) (St.~4, 99487)};
            \addplot+ [ggreen] coordinates
                {(St.~1,1) (St.~2,10) (St.~3,10) (St.~4, 10)};
        \end{scope}

        \begin{scope}[
            xshift={-0.75*\pgfplotbarwidth},
            draw=none,
        ]
            \addplot+ [bblue!50]  coordinates
                {(St.~5,99810) (St.~6,99857) (St.~7, 80000) (St.~8, 40000)};
            \addplot+ [rred!50]   coordinates
                {(St.~5,99416) (St.~6,99463) (St.~7, 79685) (St.~8, 39842)};
            \addplot+ [ggreen!50] coordinates
                {(St.~5,10) (St.~6,10) (St.~7, 10) (St.~8, 0)};
        \end{scope}

        \legend{Muster,Subgruppen,Gekürzt}
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

7
  • That looks good. The smaller bars are nearly invisible because they are so small. How can I conveniently write the total count above each bar (vertically)? Maybe that would make it more understandable.
    – robinki
    Feb 12, 2017 at 12:30
  • 1
    I would just state another value for ymin, e.g. 1e-1. Does that fit your needs as well? (Because when using nodes near coords this will overlap the values and you have another problem.) Feb 12, 2017 at 12:54
  • That should do the trick for now. Thank you. I would upvote, if I had enough reputation.
    – robinki
    Feb 12, 2017 at 13:09
  • 1
    Not a problem. What a little bit longer and just remember to to it when you have ;) Feb 12, 2017 at 14:00
  • I finally upvoted now. I used ymin = 0 in the final version, btw. You can slightly see the bar of the first stage. I will see how it looks printed.
    – robinki
    Feb 15, 2017 at 8:51

You must log in to answer this question.

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