我想一起绘制几个箱线图,但是遇到了几个问题。
每 5 个箱线图都属于同一条件,这就是为什么它们具有相同的样式(目前很糟糕,但我稍后会改进 :D)。您可能注意到,目前,当绘制 5 个箱线图块时,下一个箱线图会直接出现在 x 轴上。我想找到一种方法在每 5 个箱线图块之间添加一些空间,以便在视觉上将它们分开。我知道有命令,
draw position
但如果我包括每组 5 个箱线图都绘制在相同的位置(我注释了代码以在 MWE 中获得此结果), 但我还是想在它们之间留一点空间(我在其中一条评论中留下了这个“糟糕”的方法)。理想情况下,我希望能够控制箱线图集之间和箱线图集内的间距。此外,有没有办法
filename
以更智能的方式循环?在这个虚拟示例中,我只有 3 组数据,但实际上我有超过 3 个文件。也许使用变量来存储文件名会更好?
这是 MWE
\documentclass[border=3mm]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{statistics}
\begin{filecontents}{audio1.tsv}
37 22 79 3 100
9 60 113 1 97
29 63 65 0 99
12 21 83 0 99
7 28 78 1 97
29 -10 73 1 98
17 32 73 1 98
10 58 78 0 95
17 45 73 2 99
38 28 91 0 96
\end{filecontents}
\begin{filecontents}{speech4.tsv}
17 47 97 0 95
3 53 103 1 97
37 70 104 1 99
21 41 109 0 100
36 53 86 3 98
37 58 90 3 99
8 30 81 1 97
19 44 99 4 96
20 20 118 3 96
27 2 107 3 99
\end{filecontents}
\begin{filecontents}{music3.tsv}
26 -5 79 4 100
12 61 101 0 99
13 32 116 1 98
10 17 94 3 93
9 81 75 4 100
7 50 58 0 98
30 56 79 2 99
19 28 93 1 92
10 31 49 1 99
11 22 46 0 100
\end{filecontents}
\begin{document}
\begin{tikzpicture}
%/*
\pgfplotscreateplotcyclelist{mycolorlist}{%
{color=red},
{color=green},
{color=blue},
{color=magenta},
{color=black}
}
\pgfplotsset{% General options
width=8cm,
height=5cm,
cycle list name=mycolorlist,
boxplot/draw direction=y,xmin=0,xmax=30
}
%
\begin{groupplot}[
group style={
group name=myplot,
group size=1 by 2,
vertical sep=0pt,
horizontal sep=0pt,
},
]
\nextgroupplot[
xlabel={sdasdas},
]
\foreach \filename in {audio1,speech4,music3} {
\foreach \i in {0,...,4} {
% draw each originally row, now column as boxplot
% \addplot+ [boxplot={draw position=5*\i}] table [y index=\i] {\filename.tsv};
\addplot+ [boxplot] table [y index=\i] {\filename.tsv};
}
}
\end{groupplot}
%*/
\end{tikzpicture}
\end{document}
答案1
所以您的意思是下面这样?
% used PGFPlots v1.14
\begin{filecontents}{audio1.tsv}
37 22 79 3 100
9 60 113 1 97
29 63 65 0 99
12 21 83 0 99
7 28 78 1 97
29 -10 73 1 98
17 32 73 1 98
10 58 78 0 95
17 45 73 2 99
38 28 91 0 96
\end{filecontents}
\begin{filecontents}{speech4.tsv}
17 47 97 0 95
3 53 103 1 97
37 70 104 1 99
21 41 109 0 100
36 53 86 3 98
37 58 90 3 99
8 30 81 1 97
19 44 99 4 96
20 20 118 3 96
27 2 107 3 99
\end{filecontents}
\begin{filecontents}{music3.tsv}
26 -5 79 4 100
12 61 101 0 99
13 32 116 1 98
10 17 94 3 93
9 81 75 4 100
7 50 58 0 98
30 56 79 2 99
19 28 93 1 92
10 31 49 1 99
11 22 46 0 100
\end{filecontents}
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{
groupplots,
statistics,
}
\pgfplotscreateplotcyclelist{mycolorlist}{%
{color=red},
{color=green},
{color=blue},
{color=magenta},
{color=black}
}
\pgfplotsset{
compat=1.10,
width=8cm,
height=5cm,
cycle list name=mycolorlist,
boxplot/draw direction=y,
}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=1 by 2,
},
xmin=0,
xmax=25,
xlabel={sdasdas},
]
\nextgroupplot
% add here another variable so we can use it for calculations
\foreach \filename/\j in {
audio1/1,
speech4/2,
music3/3%
} {
\foreach \i in {0,...,4} {
% draw each originally row, now column as boxplot
\addplot+ [
boxplot={
% now calculate the positions depending on how they
% should be organized
draw position=5*\i+\j,
}
] table [y index=\i] {\filename.tsv};
}
}
\nextgroupplot
% add here another variable so we can use it for calculations
\foreach \filename/\j in {
audio1/1,
speech4/2,
music3/3%
} {
\foreach \i in {0,...,4} {
% draw each originally row, now column as boxplot
\addplot+ [
boxplot={
% now calculate the positions depending on how they
% should be organized
draw position=7*(\j-1) + \i+1,
}
] table [y index=\i] {\filename.tsv};
}
}
\end{groupplot}
\end{tikzpicture}
\end{document}