以下是修改后的版本从这里开始的箱线图(每个数据集有 4 个箱线图,而不是 2 个):
\documentclass[a4paper]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={time (s)},
height=8cm,
boxplot={
%
% Idea:
% place the
% group 1 at 0,2 + 0,4 + 0,6 + 0,8
% group 2 at 1,2 + 1,4 + 1,6 + 1,8
% group 3 at 2,2 + 2,4 + 2,6 + 2,8
% group 3 at 3,2 + 3,4 + 3,6 + 3,8
%
% in a formular:
draw position={1/5 + floor(\plotnumofactualtype/4) + 1/5*mod(\plotnumofactualtype,4)},
%
% that means the box extend must be at most 0.2 :
box extend=0.15,
},
% ... it also means that 1 unit in x controls the width:
x=3cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,50},
x tick label as interval,
xticklabels={%
{Data set 1\\{\tiny off/on}},%
{Data set 2\\{\tiny off/on}},%
{Data set 3\\{\tiny off/on}},%
{Data set 4\\{\tiny off/on}},%
},
x tick label style={
text width=2.5cm,
align=center
},
cycle list={{red},{blue},{green},{purple}},
]
\addplot
table[row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot
table[row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\end{axis}
\end{tikzpicture}
\end{document}
不幸的是,有一个箱线图的位置错误:
我预计此箱线图位于位置 3.2。为什么它位于 3.0?
[编辑] 我的解决方法是手动设置该位置:
\addplot+[boxplot={draw position=3.2}]
答案1
附录/新答案
随着 TikZ/PGF v3.1 的发布,下面提到的错误已经修复,因此 OPs 示例现在可以按预期工作。
旧答案
这是因为已经知道漏洞在mod
函数中。为了解决这个问题,我调整了最初给出的解决方案杰克。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.8}
% borrowed from <https://tex.stackexchange.com/a/145967/95441>
\pgfmathdeclarefunction{fpumod}{2}{%
\pgfmathfloatdivide{#1}{#2}%
\pgfmathfloatint{\pgfmathresult}%
\pgfmathfloatmultiply{\pgfmathresult}{#2}%
\pgfmathfloatsubtract{#1}{\pgfmathresult}%
% replaced `0' by `5' to make it work for this problem
\pgfmathfloatifapproxequalrel{\pgfmathresult}{#2}{\def\pgfmathresult{5}}{}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={time (s)},
height=8cm,
boxplot={
%
% Idea:
% place the
% group 1 at 0,2 + 0,4 + 0,6 + 0,8
% group 2 at 1,2 + 1,4 + 1,6 + 1,8
% group 3 at 2,2 + 2,4 + 2,6 + 2,8
% group 3 at 3,2 + 3,4 + 3,6 + 3,8
%
% in a formular:
draw position={
1/5 + floor(\plotnumofactualtype/4)
+ 1/5*fpumod(\plotnumofactualtype,4)
},
% that means the box extend must be at most 0.2 :
box extend=0.15,
},
% ... it also means that 1 unit in x controls the width:
x=3cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,50},
% x tick label as interval,
% xticklabels={%
% {Data set 1\\{\tiny off/on}},%
% {Data set 2\\{\tiny off/on}},%
% {Data set 3\\{\tiny off/on}},%
% {Data set 4\\{\tiny off/on}},%
% },
% x tick label style={
% text width=2.5cm,
% align=center
% },
cycle list={{red},{blue},{green},{purple}},
]
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 2
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 3
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
% block 4
\addplot table [row sep=\\,y index=0] {
data\\
60\\
516\\
710\\
503\\
1253\\
};
\addplot table [row sep=\\,y index=0] {
data\\
759\\
419\\
309\\
883\\
299\\
};
\addplot table [row sep=\\,y index=0] {
data\\
516\\
480\\
1356\\
200\\
736\\
};
\addplot table [row sep=\\,y index=0] {
data\\
684\\
340\\
700\\
325\\
377\\
};
\end{axis}
\end{tikzpicture}
\end{document}