1

I want to draw a boxplot using boxplot prepared@ Page 469 of pgfplots-manual; v1.13, with the values of lower whisker, lower quartile, median, higher quartile, and higher whisker from a data file.

The data file is as follows (without comments):

lower whisker     // the first row
lower quartile    // the second row
median            // the third row
higher quartile   // the fourth row
higher whisker    // the fifth row
outlier1          // followed by n outliers
outlier2
...
outliern

I can access the outliers using skip coords between index={0}{4}, but how can I access the data like lower whisker as follows:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{width=7cm,compat=1.13}
\usepgfplotslibrary{statistics}
\begin{tikzpicture}
\begin{axis}[
  y=1.5cm,
  skip coords between index={0}{4},  // skip the first five data
]
\addplot+[
boxplot prepared={
lower whisker= ,   // the first row
lower quartile= ,  // the second row
median= ,          // the third row
upper quartile= ,  // the fourth row
upper whisker= ,   // the fifth row
},
]
table[y index=0] {data.txt};
\end{axis}
\end{tikzpicture}
\end{document}

Note 1: This question is inspired by the answer to the post: Is it possible to user-define the percentages for lower/upper whisper in boxplot of PGFPlots?

Note 2: The related post Read boxplot prepared values from a table assumes a different data format. I cannot figure out how to apply the techniques in that post to my problem.

1 Answer 1

3

You can transpose your table using \pgfplotstabletranspose and then use the solution given in Read boxplot prepared values from a table

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotsset{width=7cm,compat=1.13}
\usepgfplotslibrary{statistics}

\makeatletter
\pgfplotsset{
    boxplot prepared from table/.code={
        \def\tikz@plot@handler{\pgfplotsplothandlerboxplotprepared}%
        \pgfplotsset{
            /pgfplots/boxplot prepared from table/.cd,
            #1,
        }
    },
    /pgfplots/boxplot prepared from table/.cd,
        table/.code={\pgfplotstablecopy{#1}\to\boxplot@datatable},
        row/.initial=0,
        make style readable from table/.style={
            #1/.code={
                \pgfplotstablegetelem{\pgfkeysvalueof{/pgfplots/boxplot prepared from table/row}}{##1}\of\boxplot@datatable
                \pgfplotsset{boxplot/#1/.expand once={\pgfplotsretval}}
            }
        },
        make style readable from table=lower whisker,
        make style readable from table=upper whisker,
        make style readable from table=lower quartile,
        make style readable from table=upper quartile,
        make style readable from table=median,
        make style readable from table=lower notch,
        make style readable from table=upper notch
}
\makeatother

\usepackage{filecontents}
\begin{filecontents}{data.txt}
5   4
7   5
8.5 6.5
9.5 8.5
10  9.5
\end{filecontents}

\begin{document}
\pgfplotstabletranspose[input colnames to=]{\datatable}{data.txt}

\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y]
  \addplot+[
  boxplot prepared from table={
    table=\datatable,
    lower whisker=0,
    upper whisker=4,
    lower quartile=1,
    upper quartile=3,
    median=2
  }, boxplot prepared
  ]
  coordinates {};

  \addplot+[
  boxplot prepared from table={
    table=\datatable,
    row=1,
    lower whisker=0,
    upper whisker=4,
    lower quartile=1,
    upper quartile=3,
    median=2
  }, boxplot prepared
  ]
  coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .