how to make vertical box plot to horizontal box plot

1,340

Package pgfplots supports box plot diagrams that are horizontal by default.

\documentclass{standalone}

\usepackage{pgf} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}

\newcommand*{\boxplot}[6]{%
  \addplot+[
    line width=.2mm,
    black,
    boxplot prepared={
      lower whisker={#5},
      lower quartile={#3},
      median={#2},
      upper quartile={#4},
      upper whisker={#6},
    }
  ]
  % No outliers
  coordinates{};
}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[%
    ymin=0, ymax=6,%
    xmin=-.01, xmax=.4,%
    ytick={1,2,3,4,5},yticklabels={D04,D10,D21,D36,D60},%
    boxplot/every median/.style={draw=red},
    ]
    % #1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile,
    % #5: min, #6: max    
    \boxplot{1}{.00479}{.001777}{.011400}{0.0000232}{0.0209}
    \boxplot{2}{.00828}{.004987}{.018975}{0.0005100}{0.1}
    \boxplot{3}{.03300}{.013950}{.088550}{0.0008580}{0.36}
    \boxplot{4}{.06708}{.034778}{.135850}{0.0060770}{0.2}
    \boxplot{5}{.06800}{.033600}{.152500}{0.0000030}{0.3}
    \end{axis}
    \end{tikzpicture}
\end{document}

Result

Share:
1,340

Related videos on Youtube

user3104352
Author by

user3104352

Updated on August 01, 2022

Comments

  • user3104352
    user3104352 over 1 year

    I have code that draws vertical box plot. I need to change it into horizontal box plot. Can you please tell me how to do it?

    \documentclass{article}
    
    \usepackage{pgf} 
    \usepackage{pgfplots}
    \usepackage[active,tightpage]{preview}
    \usetikzlibrary{fit,calc}
    \usepgfplotslibrary{external}
    \newcommand{\boxplot}[6]{%
        %#1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile, #5: min, #6: max
        \filldraw[fill=white,line width=0.2mm] let \n{boxxl}={#1-0.1}, \n{boxxr}={#1+0.1} in (axis cs:\n{boxxl},#3) rectangle (axis cs:\n{boxxr},#4);   % draw the box
        \draw[line width=0.2mm, color=red] let \n{boxxl}={#1-0.1}, \n{boxxr}={#1+0.1} in (axis cs:\n{boxxl},#2) -- (axis cs:\n{boxxr},#2);             % median
        \draw[line width=0.2mm] (axis cs:#1,#4) -- (axis cs:#1,#6);                                                                           % bar up
        \draw[line width=0.2mm] let \n{whiskerl}={#1-0.025}, \n{whiskerr}={#1+0.025} in (axis cs:\n{whiskerl},#6) -- (axis cs:\n{whiskerr},#6);        % upper quartile
        \draw[line width=0.2mm] (axis cs:#1,#3) -- (axis cs:#1,#5);                                                                           % bar down
        \draw[line width=0.2mm] let \n{whiskerl}={#1-0.025}, \n{whiskerr}={#1+0.025} in (axis cs:\n{whiskerl},#5) -- (axis cs:\n{whiskerr},#5);        % lower quartile
    }
    
    \PreviewEnvironment{tikzpicture}
    
    \begin{document}
        \tikzset{external/remake next}
        \begin{tikzpicture}
        \begin{axis}[%
        xmin=0, xmax=6,%
        ymin=-.01, ymax=.4,%
        xtick={1,2,3,4,5},xticklabels={D04,D10,D21,D36,D60}%
        ]
        %#1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile, #5: min, #6: max
        \boxplot{1}{.00479}{.001777}{.011400}{0.0000232}{0.0209}
        \boxplot{2}{.00828}{.004987}{.018975}{0.0005100}{0.1}
        \boxplot{3}{.03300}{.013950}{.088550}{0.0008580}{0.36}
        \boxplot{4}{.06708}{.034778}{.135850}{0.0060770}{0.2}
        \boxplot{5}{.06800}{.033600}{.152500}{0.0000030}{0.3}                   
        \end{axis}
        \end{tikzpicture}
    
    \end{document}
    
  • user3104352
    user3104352 about 6 years
    The code throws an error Package pgfkeys Error: I do not know the key '/pgfplots/boxplot/ lower whiskeisspelled it. ...0479}{.001777}{.011400}{0.0000232}{0.0209}
  • Heiko Oberdiek
    Heiko Oberdiek about 6 years
    @user3104352 I have used pgfplots 2016-01-06 v1.13.