![如何拆分 pgfplots 条形图中的分组条形图](https://linux22.com/image/326838/%E5%A6%82%E4%BD%95%E6%8B%86%E5%88%86%20pgfplots%20%E6%9D%A1%E5%BD%A2%E5%9B%BE%E4%B8%AD%E7%9A%84%E5%88%86%E7%BB%84%E6%9D%A1%E5%BD%A2%E5%9B%BE.png)
我确信这是一个相当琐碎的问题,但我似乎无法弄清楚。我有一张条形图,最好有三组四条条形图。问题是,使用下面的 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}
输出