我有以下箱线图,我想添加平均值

我有以下箱线图,我想添加平均值

我有以下箱线图:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [title = {Lengths of Amplification and Deletion Regions},ylabel =
        {$\log_{10}(\hbox{Length})$},
    boxplot/draw direction=y,
    xtick={1,2,3},
    xticklabels={Amplification, Deletion, Both},
    x tick label style={font=\footnotesize, text width=2.5cm, align=center}
    ]
    \addplot+[mark = *, mark options = {red},
    boxplot prepared={
      lower whisker=2.214844,
      lower quartile=3.608312,
      median=3.895478,
      average = 4.05,
      upper quartile=4.447298,
      upper whisker=4.666284
    }, color = red
    ] coordinates{(0,4.832228)(0,5.513942)(0,6.29165)(0,5.216712)(0,5.677036)(0,4.981995)(0,5.172095)},
    node at (boxplot box cs: \boxplotvalue{(average)},1),
    {\pgfmathprintnumber{\boxplotvalue{average}}};
    \addplot+[mark = *,mark options = {blue},
    boxplot prepared={
      lower whisker=3.508799,
      lower quartile=5.079821,
      median=5.481519,
      average=5.442438,
      upper quartile=5.971588,
      upper whisker=6.250831
    }, color = blue
    ] coordinates{(0,6.508115)(0,6.486354)(0,6.860059)(0,6.620663)(0,7.312391)(0,7.357306)(0,6.421694)
    (0,6.479597)(0,6.690945)(0,6.661593)(0,7.271025)(0,6.396931)(0,7.035161)(0,7.371248)(0,7.033689)
    (0,7.002645)(0,6.590617)(0,7.171933)(0,6.416259)},
    node at (boxplot box cs: \boxplotvalue{(average)},1),
    {\pgfmathprintnumber{\boxplotvalue{average}}};
    \addplot+[mark = *,mark options = {green},
    boxplot prepared={
      lower whisker=2.437751,
      lower quartile=3.334956,
      median=4.336029,
      average = 4.47,
      upper quartile=5.068459,
      upper whisker=5.265037
    }, color = green
    ] coordinates{(0,5.826492)(0,5.819791)(0,6.21436)},
    node at (boxplot box cs: \boxplotvalue{(average)},1),
    {\pgfmathprintnumber{\boxplotvalue{average}}};

    \end{axis}
\end{tikzpicture}
\end{document}

这使:

在此处输入图片描述

我得到了其他社区成员的帮助,也阅读了 pgfplots 手册。但是,我遇到了难题:我想将每个平均值打印在相应的菱形上方。我的节点哪里做错了?谢谢!

答案1

  • 存在语法错误,行尾(规范之后coordinates和语句中间)有几个逗号node

  • \boxplotvalue{<key>}期望键不带额外的括号。

  • node[above]将节点放置在指定坐标上方。

  • 0.5而不是1将节点置于坐标系的中间box plot

完整示例:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [title = {Lengths of Amplification and Deletion Regions},ylabel =
        {$\log_{10}(\hbox{Length})$},
    boxplot/draw direction=y,
    xtick={1,2,3},
    xticklabels={Amplification, Deletion, Both},
    x tick label style={font=\footnotesize, text width=2.5cm, align=center}
    ]
    \addplot+[mark = *, mark options = {red},
    boxplot prepared={
      lower whisker=2.214844,
      lower quartile=3.608312,
      median=3.895478,
      average = 4.05,
      upper quartile=4.447298,
      upper whisker=4.666284
    }, color = red
    ]
    coordinates{(0,4.832228)(0,5.513942)(0,6.29165)(0,5.216712)(0,5.677036)(0,4.981995)(0,5.172095)}
    node[above] at (boxplot box cs: \boxplotvalue{average}, .5)
    {\boxplotvalue{average}}
    ;
    \addplot+[mark = *,mark options = {blue},
    boxplot prepared={
      lower whisker=3.508799,
      lower quartile=5.079821,
      median=5.481519,
      average=5.442438,
      upper quartile=5.971588,
      upper whisker=6.250831
    }, color = blue
    ]
    coordinates{(0,6.508115)(0,6.486354)(0,6.860059)(0,6.620663)(0,7.312391)(0,7.357306)(0,6.421694)
    (0,6.479597)(0,6.690945)(0,6.661593)(0,7.271025)(0,6.396931)(0,7.035161)(0,7.371248)(0,7.033689)
    (0,7.002645)(0,6.590617)(0,7.171933)(0,6.416259)}
    node[above] at (boxplot box cs: \boxplotvalue{average}, .5)
    {\boxplotvalue{average}};
    \addplot+[mark = *,mark options = {green},
    boxplot prepared={
      lower whisker=2.437751,
      lower quartile=3.334956,
      median=4.336029,
      average = 4.47,
      upper quartile=5.068459,
      upper whisker=5.265037
    }, color = green
    ] coordinates{(0,5.826492)(0,5.819791)(0,6.21436)}
    node[above] at (boxplot box cs: \boxplotvalue{average}, .5)
    {\boxplotvalue{average}};
    \end{axis}
\end{tikzpicture}
\end{document}

结果

相关内容