Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am new to LaTeX. How can I draw a boxplot in LaTeX. My data set is as below

index median box_top box_bottom whisker_top whisker_bottom
0 1 1.2 0.4 1.5 0.2
1 2 2.3 1.5 2.7 1
2 0.7 1.4 0.5 1.9 0.1

I am using Kile on Ubuntu 12.04 LTS.

EDIT: Code is added with output.

    \documentclass{minimal}
    \usepackage{pgfplots}
    \usepackage{filecontents}

    \begin{filecontents}{testdata.dat}
     0 1 1.2 0.4 1.5 0.2
     1 2 2.3 1.5 2.7 1
     2 0.7 1.4 0.5 1.9 0.1
     3 1 1.2 0.4 1.5 0.2
     4 2 2.3 1.5 2.7 1
     5 0.7 1.4 0.5 1.9 0.1
    \end{filecontents}

    \pgfplotsset{
     box plot width/.initial=1em,
     box plot/.style={
        /pgfplots/.cd,
        black,
        only marks,
        mark=-,
        mark size=\pgfkeysvalueof{/pgfplots/box plot width},
        /pgfplots/error bars/.cd,
        y dir=plus,
        y explicit,
     },
     box plot box/.style={
        /pgfplots/error bars/draw error bar/.code 2 args={%
            \draw  ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
        },
        /pgfplots/table/.cd,
        y index=2,
        y error expr={\thisrowno{3}-\thisrowno{2}},
        /pgfplots/box plot
     },
     box plot top whisker/.style={
        /pgfplots/error bars/draw error bar/.code 2 args={%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
            {\pgfplotserrorbarsmark}%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
            {\pgfplotserrorbarsmarkopts}%
            \path ##1 -- ##2;
        },
        /pgfplots/table/.cd,
        y index=4,
        y error expr={\thisrowno{2}-\thisrowno{4}},
        /pgfplots/box plot
     },
     box plot bottom whisker/.style={
        /pgfplots/error bars/draw error bar/.code 2 args={%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark}%
            {\pgfplotserrorbarsmark}%
            \pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
            {\pgfplotserrorbarsmarkopts}%
            \path ##1 -- ##2;
        },
        /pgfplots/table/.cd,
        y index=5,
        y error expr={\thisrowno{3}-\thisrowno{5}},
        /pgfplots/box plot
     },
     box plot median/.style={
        /pgfplots/box plot
     }
    }

    \begin{document}
    \begin{tikzpicture}
    \begin{axis} [enlarge x limits=0.5,xtick=data, box plot width=0.5em]
     \addplot [box plot median] table {testdata.dat};
     \addplot [box plot box] table {testdata.dat};
     \addplot [box plot top whisker] table {testdata.dat};
     \addplot [box plot bottom whisker] table {testdata.dat};
    \end{axis}
    \end{tikzpicture}
    \end{document}

enter image description here

EDIT 2: here is the actual data I am trying to plot

%index median box_top box_bottom whisker_top whisker_bottom
0.52 21.5 191.5 4 150842.5 3
0.53 17 71.5 3 30840.5 2
0.54 5 1 2 2799 1
0.55 9 54 3 2425 2
0.56 4 11.5 2 643.5 1
0.57 3 10 2 124 1
share|improve this question

migrated from stackoverflow.com May 20 '13 at 12:42

This question came from our site for professional and enthusiast programmers.

    
this blog may help you blog.ufes.br/fabiomolinares/category/latex –  hob May 20 '13 at 8:03
    
I tried this, it is giving me vertical V as drawing –  pratibha May 20 '13 at 8:48
    
How about tex.stackexchange.com/questions/84541/… –  Uwe Ziegenhagen May 20 '13 at 12:51
    
Welcome to TeX.SX! Your post was migrated here from another Stack Exchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. –  Jubobs May 20 '13 at 12:55
    
Related: tex.stackexchange.com/questions/3983/… –  Jubobs May 20 '13 at 13:01

1 Answer 1

Boxplots are described in chapter 5.9.1 of the PGFPLOTS manual.

Using your data with the examples in the manual I get:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [
    ytick={1,2,3},
    yticklabels={Index 0, Index 1, Index 2},
    ]
    \addplot+[
    boxplot prepared={
      median=1,
      upper quartile=1.2,
      lower quartile=0.4,
      upper whisker=1.5,
      lower whisker=0.2
    },
    ] coordinates {};
    \addplot+[
    boxplot prepared={
      median=2,
      upper quartile=2.3,
      lower quartile=1.5,
      upper whisker=2.7,
      lower whisker=1
    },
    ] coordinates {};
    \addplot+[
    boxplot prepared={
      median=0.7,
      upper quartile=1.4,
      lower quartile=0.5,
      upper whisker=1.9,
      lower whisker=0.1
    },
    ] coordinates {};
  \end{axis}
\end{tikzpicture}

\end{document}

share|improve this answer
    
Stil a zigzag line. –  pratibha Jun 11 '13 at 8:36
    
You need version 1.8 of pgfplots. It won't work with earlier versions. –  Håkon Malmedal Jun 13 '13 at 18:26
    
How can I check version of pgfplot on my system? –  pratibha Jun 18 '13 at 7:04
    
Open a terminal window and type texdoc pgfplots. This will open a PDF file with the documentation. The version number is listed on the first page of the documentation. –  Håkon Malmedal Jun 19 '13 at 7:53
    
or else add \listfiles before you \begin{document} and check the log file –  cmhughes Jun 24 '13 at 14:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.