pgfplots:缺少一组条形图

pgfplots:缺少一组条形图

我正在尝试绘制一个条形图,其中有 4 个值组,每个值组由 5 个条形组成。我认为这是一个缩放问题,所以没有绘制所有组。我已经看过文档,但我找不到正确的配置来实现这一点……

\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

正如ferahfeza在在问题下方评论最后一个条形图块没有显示的原因在于ybar interval声明。这里是为什么这是因为间隔默认情况下将显示为,xticklabels并且间隔也需要最终值它从最后一个数据点获取数据(或者这可能更容易理解,从数据表中的最后一个数据 x 值获取)。

有关详细信息,请查看代码中的注释。

% 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}

该图显示了上述代码的结果

相关内容