2

I have a need to wrap the `\addplot [boxplot prepared]' inside a macro to greatly save typing. It seems simple enough, but the devil is in the details with macro expansion issues. Is this doable given PGFPLOTS ? I looked through the Programming Notes as well as the PGFPLOTS 1.11 Manual, with nothing coming up. I tried dumping the macro to the log file with the \meaning command. It gives me a 'Invalid character' message for every character in the macro. Not much use there. Any insights would be greatly appreciated. Its frustrating something so fundamentally simple would bring a project to a dead stop. My MWE is below.

\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
  • Welcome at Tex.SX! You encountered a bug in pgfplots, sorry for that. I have fixed it; it will be gone in the next stable release. The workaround of @Jake repairs it. – Christian Feuersänger Oct 28 '14 at 18:50
2

You need to add a % at the end of the \addplot[ boxplot prepared={ line.

\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}
3
  • Thanks for your post. It still fails with this: ! Package pgfkeys Error: I do not know the key '/pgfplots/boxplot/ lower whiske'. Seems like text is being lost in the expansion. – user50506 Oct 28 '14 at 9:09
  • @user50506: Try eliminating the newline completely: \addplot[boxplot prepared={lower whisker={#1},.... – Jake Oct 28 '14 at 9:55
  • Jake, thanks for your help. I first tried the % after the first line with no success. After applying to the second line as well...success! Christian posted there is a bug here. Your help is greatly appreciated. – user50506 Oct 29 '14 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.