连接不同箱线图中值的线段

连接不同箱线图中值的线段

我有一个包含多个箱线图的图,按不同条件分组(5×5),如图所示

在此处输入图片描述

每组 5 个箱线图基于单个文件中定义的数据。我想添加一条线来连接每组箱线图的中位数;像这样

在此处输入图片描述

这是我的数据集的一个虚拟子集,它更大。因此,我想依靠一种不需要我直接输入不同中位数的方法,而是根据用于绘制箱线图的文件来计算它们。有没有简单的方法可以做到这一点?如果我可以决定要连接多少个箱线图,例如,只连接每组五个中的前三个,而不是全部五个,那将是一个加分项。

这是用于生成第一个图的 MWE

% 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=20,
        xtick={3,10,17},
        xticklabels={a,b,c},
        xlabel={something-x},
        ylabel={something-y},
    ]
    \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} 

答案1

稍微迂回地做这件事,因此可能会有更优雅的解决方案。

我在中线中间创建命名坐标,并使用\draw之外的法线groupplot绘制中线之间的线。若要仅连接每组中的前三个,请更改{1,...,4}{1,...,2}

代码输出

% 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}
%\usetikzlibrary{
    \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=20,
        xtick={3,10,17},
        xticklabels={a,b,c},
        xlabel={something-x},
        ylabel={something-y},
    ]
    \nextgroupplot
        % add here another variable so we can use it for calculations
        \foreach[count=\j] \filename in {
            audio1,
            speech4,
            music3%
        } {
            \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}
                 node (tmp)  at (boxplot box cs:\boxplotvalue{median},0.5) {}
                 ;
             \edef\tmp{\noexpand\coordinate (m-\j-\i) at (tmp.center);}
             \tmp
            }
        }
    \end{groupplot}

\foreach \j in {1,2,3}
  \draw [thick] (m-\j-0) foreach \i in {1,...,4} { -- (m-\j-\i) };
\end{tikzpicture}
\end{document} 

相关内容