Graphics, Figures & Tablespgfplots with pgfmath and \newcommand

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
joerghe
Posts: 1
Joined: Wed Sep 08, 2010 9:49 am

pgfplots with pgfmath and \newcommand

Post by joerghe »

I'd like to draw box-and-whisker-plots using pgf. As I could not find any intrinsic command to do so, I desided to do it by myself. My ideas was to create a new command called \boxplot which takes 6 arguments (center, median, 1/4 quartile 3/4 quartile, min, max) and creates one box-and-whisker. To compute the width of the box and the whiskers I use pgfmath.

Code: Select all

\documentclass{report}

\usepackage{pgf,pgfplots}

\usetikzlibrary{fit,calc}
\usepgfplotslibrary{external}

\newcommand{\boxplot}[6]{%
    %#1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile, #5: min, #6: max
    \pgfmathparse{#1+0.25}\edef\boxxr{\pgfmathresult}%
    \pgfmathparse{#1-0.25}\edef\boxxl{\pgfmathresult}%
    \pgfmathparse{#1+0.15}\edef\whiskerr{\pgfmathresult}%
    \pgfmathparse{#1-0.15}\edef\whiskerl{\pgfmathresult}%
    \filldraw[fill=green!20,line width=0.2mm] (axis cs:\boxxl,#3) rectangle (axis cs:\boxxr,#4);% draw the box
    \draw[line width=0.2mm, color=red] (axis cs:\boxxl,#2) -- (axis cs:\boxxr,#2);              % median
    \draw[line width=0.2mm] (axis cs:#1,#4) -- (axis cs:#1,#6);                                 % bar up
    \draw[line width=0.2mm] (axis cs:\whiskerl,#6) -- (axis cs:\whiskerr,#6);                   % upper quartile
    \draw[line width=0.2mm] (axis cs:#1,#3) -- (axis cs:#1,#5);                                 % bar down
    \draw[line width=0.2mm] (axis cs:\whiskerl,#5) -- (axis cs:\whiskerr,#5);                   % lower quartile
}

\begin{document}

\tikzset{external/remake next}
\begin{tikzpicture}
\begin{axis}[xmin=0.5, xmax=2.5, ymin=4, ymax=16,
             xtick={1,2},
             xticklabels={1,2}]
%#1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile, #5: min, #6: max
\boxplot{1}{12.5}{12}{13}{10}{15}
\boxplot{2}{6.5}{6}{7}{5}{8}
\end{axis}
\end{tikzpicture}

\end{document} 
Now my problem: when I call \boxplot twice the x-positions of the first box-and-whisker (that are computed using pgfmath) are put to the x-positions of the second box-and-whisker (please refer to the attached pdf-file generated with the code above).

If someone could help me with this problem, I'd very appreciate.
Attachments
Example.pdf
(10.93 KiB) Downloaded 570 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

fluffyk
Posts: 1
Joined: Mon Jan 24, 2011 5:44 pm

pgfplots with pgfmath and \newcommand

Post by fluffyk »

Probably less efficient, but it does work:

Code: Select all

    \documentclass{report}

    \usepackage{pgf,pgfplots}

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

    \begin{document}

    \tikzset{external/remake next}
    \begin{tikzpicture}
    \begin{axis}[xmin=0.5, xmax=2.5, ymin=4, ymax=16,
                 xtick={1,2},
                 xticklabels={1,2}]
    %#1: center, #2: median, #3: 1/4 quartile, #4: 3/4 quartile, #5: min, #6: max
    \boxplot{1}{12.5}{12}{13}{10}{15}
    \boxplot{2}{6.5}{6}{7}{5}{8}
    \end{axis}
    \end{tikzpicture}

    \end{document}

Post Reply