如何将垂直箱线图转换为水平箱线图

如何将垂直箱线图转换为水平箱线图

我有绘制垂直箱线图的代码。我需要将其更改为水平箱线图。你能告诉我怎么做吗?

\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}

答案1

软件包pgfplots默认支持水平的箱线图。

\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

相关内容