Skip to content

Instantly share code, notes, and snippets.

@rschrieken
Created July 20, 2017 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rschrieken/66ccd5743c6a6c27b3564420918912f3 to your computer and use it in GitHub Desktop.
Save rschrieken/66ccd5743c6a6c27b3564420918912f3 to your computer and use it in GitHub Desktop.
test
% How to draw boxplots with TikZ. Adapted from
% “fluffyk”s example in the LaTeX community forum: http://is.gd/cfSJai
\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}{.0209}
\boxplot{2}{.00828}{.004987}{.018975}{0.0005100}{.1}
\boxplot{3}{.03300}{.013950}{.088550}{0.0008580}{1.06}
\boxplot{4}{.06708}{.034778}{.135850}{0.0060770}{5.08}
\boxplot{5}{.06800}{.033600}{.152500}{0.0000030}{1.51}
\end{axis}
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment