Draw a boxplot from a specific line of my data set

1,151

As Symbol 1 already suggested in the comment below the question PGFPlotsTable is able to transpose the data with the \pgfplotstabletranspose command (which also was posted by Jake already here. Then you can just use a simple \foreach loop to draw the boxplots.

% used PGFPlots v1.14
\begin{filecontents*}{trial.dat}
    1 12 3 2 25 6 21 8 6
    11 2 23 14 15 16 17 82 92
    11 22 39 43 51 61 79 80 9
    1 22 3 48 35 66 17 28 99
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}                      % to transpose the table
    \usetikzlibrary{pgfplots.statistics}        % to draw `boxplot's
    \pgfplotstabletranspose\mytable{trial.dat}  % transpose the data table
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \foreach \i in {1,...,4} {
                % draw each originally row, now column as boxplot
                \addplot+ [boxplot] table [y index=\i] {\mytable};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

Share:
1,151
user7137
Author by

user7137

Updated on December 16, 2020

Comments

  • user7137
    user7137 almost 3 years

    I am new to PGFPlots and boxplot. My data set is as below:

    \begin{filecontents}{trial.dat}
    1 12 3 2 25 6 21 8 6
    11 2 23 14 15 16 17 82 92
    11 22 39 43 51 61 79 80 9
    1 22 3 48 35 66 17 28 99
    \end{filecontents}
    

    I want to draw a boxplot for, let’s say line number 2 of my data set and I want that PGFPlots calculate for me the median, the lower quartile, etc.

    As far as I understand how it works, I need to use the boxplot handler that accepts a one-dimensional sample as input. How could I say to boxplot that I want to take the one-dimensional sample from and only from line number 2 of my data set?

    (Of course, the next step for me will be to draw a boxplot for every line of my data set but it will probably be just a matter of loop).

    • Symbol 1
      Symbol 1 almost 7 years
      pgfplotstable provides some interfaces to read your data.