我如何设置我的数据以便我可以创建 pgfplots 箱线图并更新多个系列的 x 索引?

我如何设置我的数据以便我可以创建 pgfplots 箱线图并更新多个系列的 x 索引?

我一直在使用这个箱线图最近,我使用代码取得了很好的效果。我正在论文中制作大量图表,并希望能够执行以下数据格式:

\begin{filecontents}{dataOne.dat}
%x whiskerbottom boxbottom median boxtop whiskertop 
 0  -0.573413   -0.528268   -0.641948 -0.483655 -0.794368   
 1  -0.673413   -0.628268   -0.741948 -0.583655 -0.894368       
\end{filecontents}

ETC。

\boxplot定义方式如下:

\newcommand{\boxplot}[2][]{
    \addplot [box plot median,#1] table {#2};
    \addplot [forget plot, box plot box,#1] table {#2};
    \addplot [forget plot, box plot top whisker,#1] table {#2};
    \addplot [forget plot, box plot bottom whisker,#1] table {#2};
}

我使用以下命令将这些添加到我的图中:

\begin{tikzpicture}
\begin{axis} [xtick={0,1},  xmin=-1,  xmax =2,  xticklabels={test,test2},   box plot width=5mm]
\boxplot [black, forget plot]{dataOne.dat}
\addplot[color = black, mark = *, nodes near coords,every node near coord/.style={anchor=180}]coordinates {( 0, -1)};

\end{axis}
\end{tikzpicture}

我如何使用\boxplot命令让我指定仅使用表的特定行,例如:

\boxplot [black, forget plot]{dataOne.dat,0}  %only use row 0
\boxplot [black, forget plot]{dataOne.dat}  % use entire datafile

因此如果传递,它将仅引用 dataOne.dat 中的第一个数据行。否则,我必须创建单独的数据文件才能查看一个或两个箱线图。

我知道我需要修改:

table{#2}

拥有某种通过创建传递的条件参数\boxplot,但我真的不知道该怎么做。文档在这里远远超出了我的理解范围,似乎只专注于检索指定列的特定值。

答案1

您可以使用x filter/.code检查当前是否\coordindex等于所需的行号,如果不相等,则丢弃该坐标:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{dataOne.dat}
%x whiskerbottom boxbottom median boxtop whiskertop 
 0  -0.573413   -0.528268   -0.641948 -0.483655 -0.794368   
 1  -0.673413   -0.628268   -0.741948 -0.583655 -0.894368       
\end{filecontents}

\pgfplotsset{
    box plot/.style={
        /pgfplots/.cd,
        black,
        only marks,
        mark=-,
        mark size=\pgfkeysvalueof{/pgfplots/box plot width},
        /pgfplots/error bars/y dir=plus,
        /pgfplots/error bars/y explicit,
        /pgfplots/table/x index=\pgfkeysvalueof{/pgfplots/box plot x index},
    },
    box plot box/.style={
        /pgfplots/error bars/draw error bar/.code 2 args={%
            \draw  ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
        },
        /pgfplots/table/.cd,
        y index=\pgfkeysvalueof{/pgfplots/box plot box top index},
        y error expr={
            \thisrowno{\pgfkeysvalueof{/pgfplots/box plot box bottom index}}
            - \thisrowno{\pgfkeysvalueof{/pgfplots/box plot box top index}}
        },
        /pgfplots/box plot
    },
    box plot top whisker/.style={
        /pgfplots/error bars/draw error bar/.code 2 args={%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
            {\pgfplotserrorbarsmark}%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
            {\pgfplotserrorbarsmarkopts}%
            \path ##1 -- ##2;
        },
        /pgfplots/table/.cd,
        y index=\pgfkeysvalueof{/pgfplots/box plot whisker top index},
        y error expr={
            \thisrowno{\pgfkeysvalueof{/pgfplots/box plot box top index}}
            - \thisrowno{\pgfkeysvalueof{/pgfplots/box plot whisker top index}}
        },
        /pgfplots/box plot
    },
    box plot bottom whisker/.style={
        /pgfplots/error bars/draw error bar/.code 2 args={%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
            {\pgfplotserrorbarsmark}%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
            {\pgfplotserrorbarsmarkopts}%
            \path ##1 -- ##2;
        },
        /pgfplots/table/.cd,
        y index=\pgfkeysvalueof{/pgfplots/box plot whisker bottom index},
        y error expr={
            \thisrowno{\pgfkeysvalueof{/pgfplots/box plot box bottom index}}
            - \thisrowno{\pgfkeysvalueof{/pgfplots/box plot whisker bottom index}}
        },
        /pgfplots/box plot
    },
    box plot median/.style={
        /pgfplots/box plot,
        /pgfplots/table/y index=\pgfkeysvalueof{/pgfplots/box plot median index}
    },
    box plot width/.initial=1em,
    box plot x index/.initial=0,
    box plot median index/.initial=1,
    box plot box top index/.initial=2,
    box plot box bottom index/.initial=3,
    box plot whisker top index/.initial=4,
    box plot whisker bottom index/.initial=5,
    only row/.style={
            x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{nan}\fi}
    }
}

\newcommand{\boxplot}[2][]{
    \addplot [box plot median,#1] table {#2};
    \addplot [forget plot, box plot box,#1] table {#2};
    \addplot [forget plot, box plot top whisker,#1] table {#2};
    \addplot [forget plot, box plot bottom whisker,#1] table {#2};
}

\begin{document}
\begin{tikzpicture}
\begin{axis} [xtick={0,1},  xmin=-1,  xmax =2,  xticklabels={test,test2},   box plot width=5mm]
\boxplot [black, forget plot, only row=0]{dataOne.dat}
\boxplot [red, forget plot, only row=1]{dataOne.dat}
\addplot[color = black, mark = *, nodes near coords,every node near coord/.style={anchor=180}]coordinates {( 0, -1)};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容