3

I'm trying to draw a bar diagram with 4 value groups each consisting of 5 bars. I think it's a scaling problem, so not all the groups were drawn. I already had a look at the documentation but I couldn't find the right config to make that happen...

\documentclass[tikz, border=10pt]{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.16,
x tick label style={font=\tiny,align=center},
y tick label style={font=\tiny},%
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
ybar,%
enlargelimits=0.05,%
title={title},%
symbolic x coords={a,b,c,d},%
xtick={data},%
ylabel={y-label},%
xlabel={},%
xticklabels={ValueGroup 1,ValueGroup 2,ValueGroup 3,ValueGroup 4},
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
ybar interval=0.7,
]

\addplot coordinates{(a,34) (b,36) (c,70) (d,65)};
\addplot coordinates{(a,45) (b,45) (c,43) (d,43)};
\addplot coordinates{(a,23) (b,32) (c,22) (d,20)};
\addplot coordinates{(a,76) (b,25) (c,35) (d,25)};
\addplot coordinates{(a,22) (b,12) (c,45) (d,34)};

\legend{\tiny{bar 1},\tiny{bar 2},\tiny{bar 3},\tiny{bar 4},\tiny{bar 5}}
\end{axis}

\end{tikzpicture}
\end{document}

1
  • 3
    I don't know why, but if I removed the ybar interval=0.7 option from axis environment and set the enlargelimits=0.15, no problem. In the meantime, add width = 12cm or close value to pgfplotsset – user31034 Aug 24 '19 at 17:17
6

As ferahfeza already mentioned in the comment below the question the reason, that the last block of bars isn't shown, is the ybar interval statement. And here is why it so: That is because of the interval which by default would be shown as xticklabels and an interval also needs an end value which it takes from the last data point (or perhaps this is easier to understand, from the last data x value in the data table).

For details have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.3}
    % it is much simpler to provide the data in a table, which could also be
    % moved to a data file
    % (doing it this way requires to load the `pgfplotstable` package)
    % (because the x values contain a space, the entries need to be bracketed)
    \pgfplotstableread{
        x               y1  y2  y3  y4  y5
        {ValueGroup 1}  34  45  23  76  22
        {ValueGroup 2}  36  45  32  25  12
        {ValueGroup 3}  70  43  22  35  45
        {ValueGroup 4}  65  43  20  25  34
    }\datatable
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % you need to play a bit with the marked values to avoid that the
        % `xticklabels` overlap
        width=12cm,                     % <-- play with this value!
        % (in case you don't want to change the `height` you also need to
        %  (re)set this value, which otherwise would be scaled, too)
        height=\axisdefaultheight,
        ybar,
        /pgf/bar width={10pt},          % <-- play with this value! (default: 10pt)
        % (so that the different height of the bars are interpreted right,
        %  I personally prefer to let the bars start at y = 0)
        ymin=0,
        title={title},
        xtick={data},
        ylabel={y label},
        % providing the data as a table makes it easy to provide the
        % `xticklabels`
        xticklabels from table={\datatable}{x},
        x tick label style={font=\tiny},
        y tick label style={font=\tiny},
        % depending on the chosen `width` and `bar width` you need to adjust this value
        enlarge x limits={0.2},         % <-- play with this value!
        enlarge y limits={upper=0.05},
        legend style={
            at={(0.5,-0.15)},
            anchor=north,
            legend columns=-1,
            % (added this option here instead of to each `legend` entry)
            font=\tiny,
        },
        % (provided the common `\addplot` option here)
        table/x expr=\coordindex,
    ]

        % adding the `\addplot`s is very easy now
        \foreach \i in {1,...,5} {
            \addplot table [y index=\i] {\datatable};
        }

        \legend{
            bar 1,
            bar 2,
            bar 3,
            bar 4,
            bar 5,
        }
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.