PGFPLOTS:如何将 \addplot boxplot 准备好放入宏定义中

PGFPLOTS:如何将 \addplot boxplot 准备好放入宏定义中

我需要将“\addplot [boxplot ready]”包装在宏中,以大大节省输入时间。这看起来很简单,但魔鬼就在宏扩展问题的细节中。考虑到 PGFPLOTS,这可行吗?我查看了编程说明以及 PGFPLOTS 1.11 手册,但没有任何结果。我尝试使用 \meaning 命令将宏转储到日志文件中。它为宏中的每个字符都提供了“无效字符”消息。没什么用。任何见解都将不胜感激。令人沮丧的是,如此简单的事情会让项目陷入停滞。我的 MWE 如下。

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{statistics}

\newcommand{\desboxplot}[6]{
 \addplot[ boxplot prepared={
  lower whisker={#1}, lower quartile={#2}, median={#3},
  upper quartile={#4}, upper whisker={#5}, average={#6},
  box extend=0.5,  % height of box
  whisker extend=0.5, % height of whiskers
  every box/.style={thin,draw=black,fill=gray!50},
  every whisker/.style={black,thick},
  every median/.style={black,thick},
  every average/.style={draw=red, /tikz/mark=* },
  /pgf/number format/precision=2 } ]
  coordinates {}
  node[left,black] at
  (boxplot box cs: \boxplotvalue{lower whisker},0.5)
  {\tiny\pgfmathprintnumber{\boxplotvalue{lower whisker}}}
  node[right,black] at
  (boxplot box cs: \boxplotvalue{upper whisker},0.5)
  {\tiny\pgfmathprintnumber{\boxplotvalue{upper whisker}}};
} %end of \desboxplot definition

\begin{document}
\begin{tikzpicture}
\begin{axis}[
y=1.5cm,
]
\addplot+[
  boxplot prepared={
    lower whisker=5,
    lower quartile=7,
    median=8.5,
    upper quartile=9.5,
    upper whisker=10,
    box extend=2,  % height of box
    whisker extend=2.2, % height of whiskers
    every box/.style={very thick,dashed,draw=black,fill=yellow},
    every whisker/.style={red,ultra thick},
    every median/.style={densely dotted,cyan,ultra thick},
  },
]
table[row sep=\\,y index=0] {
data\\ 1\\ 3\\
};
\meaning\desboxplot %{5}{7}{8.5}{9.5}{10}{9}
\end{axis}
\end{tikzpicture}
\end{document}

答案1

%您需要在行尾添加\addplot[ boxplot prepared={

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}

\newcommand{\desboxplot}[6]{%
 \addplot[ boxplot prepared={%
  lower whisker={#1}, lower quartile={#2}, median={#3},
  upper quartile={#4}, upper whisker={#5}, average={#6},
  box extend=0.5,  % height of box
  whisker extend=0.5, % height of whiskers
  every box/.style={thin,draw=black,fill=gray!50},
  every whisker/.style={black,thick},
  every median/.style={black,thick},
  every average/.style={draw=red, /tikz/mark=* },
  /pgf/number format/precision=2 } ]
  coordinates {}
  node[left,black] at
  (boxplot box cs: \boxplotvalue{lower whisker},0.5)
  {\tiny\pgfmathprintnumber{\boxplotvalue{lower whisker}}}
  node[right,black] at
  (boxplot box cs: \boxplotvalue{upper whisker},0.5)
  {\tiny\pgfmathprintnumber{\boxplotvalue{upper whisker}}};
} %end of \desboxplot definition

\begin{document}
\begin{tikzpicture}
\begin{axis}[
y=1.5cm,
]

\desboxplot{5}{7}{8.5}{9.5}{10}{9}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容