如何拆分 pgfplots 条形图中的分组条形图

如何拆分 pgfplots 条形图中的分组条形图

我确信这是一个相当琐碎的问题,但我似乎无法弄清楚。我有一张条形图,最好有三组四条条形图。问题是,使用下面的 MWE,每组的四条条形图都堆叠在一起,而不是整齐地挨着。我无法从类似问题的 MWE 中找到任何重大变化,而这些问题似乎确实有我想要的(即分组条形图)。

MWE 在这里:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            symbolic x coords={tree,indentation,parentheses},
            ytick = {0,5000,10000,15000,20000,25000,30000,35000},
            ymin = 0,
            ymajorgrids = true,
            ymax=35000,
            scaled ticks= false,
            ylabel = {Average response time(ms)},
            xtick=data]
            %Average
            \addplot[ybar,fill=gray, mark=none] coordinates {
                (tree,20962)
                (indentation,24509)
                (parentheses,33054)
            };
            %Organizational
            \addplot[ybar,fill=blue] coordinates {
                (tree,22060)
                (indentation,21255)
                (parentheses,34917)
            };
            %Folder
            \addplot[ybar,fill=black] coordinates {
                (tree,17776)
                (indentation,15050)
                (parentheses,27904)
            };
            %Arithmetics
            \addplot[ybar,fill=green] coordinates {
                (tree,23599)
                (indentation,22923)
                (parentheses,17693)
            };
        \end{axis}

    \end{tikzpicture}
    \caption{Average response time for each representation type}
    \label{fig:avg_reptime}
\end{figure}

\end{document}

也许值得注意的是,我正在使用兼容版本 1.13(因此 \pgfplotsset{compat=1.13}。

提前致谢。

答案1

在你的情况下,你需要ybar作为选项axis而不是\addplot

您还需要enlarge x limits放大 x 轴。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}

\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \begin{axis}[
            symbolic x coords={tree,indentation,parentheses},
            ytick = {0,5000,10000,15000,20000,25000,30000,35000},
            ymin = 0,
            ymajorgrids = true,
            ymax=35000,
            ybar,
            scaled ticks= false,
            ylabel = {Average response time(ms)},
            xtick=data,
            enlarge x limits=0.2]
            %Average
            \addplot[fill=gray, mark=none] coordinates {
                (tree,20962)
                (indentation,24509)
                (parentheses,33054)
            };
            %Organizational
            \addplot[fill=blue] coordinates {
                (tree,22060)
                (indentation,21255)
                (parentheses,34917)
            };
            %Folder
            \addplot[fill=black] coordinates {
                (tree,17776)
                (indentation,15050)
                (parentheses,27904)
            };
            %Arithmetics
            \addplot[fill=green] coordinates {
                (tree,23599)
                (indentation,22923)
                (parentheses,17693)
            };
        \end{axis}

    \end{tikzpicture}
    \caption{Average response time for each representation type}
    \label{fig:avg_reptime}
\end{figure}

\end{document}

输出

在此处输入图片描述

相关内容