2

I am trying to have a boxplot from data , with two lines and frequency in the second line. example: enter image description here

I don't know how to do it, with a "foreach " or special command for boxplot ( pgfplots )

\documentclass[png,tikz,border=30pt]{standalone}
 \documentclass{article}
 \usepackage{pgfplots}
 \pgfplotsset{compat=1.8}% version
 \usetikzlibrary{pgfplots.statistics}

 \begin{document}

\begin{tabular}{|c|c|c|c|c|c|c|}\hline  centre de classe & 0.5 & 1.5  &  2.5 &  3.5 &  4.5 & 5.5
                          \\\hline effectif   &  2  &3 &4  &5  &2
                        & 1\\\hline

                        \end{tabular}


  % % % % % % %
  % % % % % % % %  i need to do this % % % % % % %
    \begin{tikzpicture}
  \begin{axis}[y=1.5cm, ymax=2]
  \addplot+[boxplot]
  table[row sep=\\,y index=0] {
      data\\
      0.5 \\ 0.5 \\ 1.5  \\ 1.5  \\1.5  \\ 2.5 \\ 2.5 \\2.5 \\2.5 \\  3.5\\ 3.5\\3.5\\3.5\\3.5\\  4.5 \\  4.5 \\5.5 \\                            
  }
  [above]
  node at
    (boxplot box cs: \boxplotvalue{lower whisker},1)
    {\pgfmathprintnumber{\boxplotvalue{lower whisker}}}
  node at
    (boxplot box cs: \boxplotvalue{lower quartile},1)
    {\pgfmathprintnumber{\boxplotvalue{lower quartile}}}
  node[left] at
    (boxplot box cs: \boxplotvalue{median},0.5)
    {\pgfmathprintnumber{\boxplotvalue{median}}}
  node at
    (boxplot box cs: \boxplotvalue{upper quartile},1)
    {\pgfmathprintnumber{\boxplotvalue{upper quartile}}}
  node at
    (boxplot box cs: \boxplotvalue{upper whisker},1)
    {\pgfmathprintnumber{\boxplotvalue{upper whisker}}}
  ;
  \end{axis}
  \end{tikzpicture}

\end{document}

I tried with for each :

\usepackage[francais]{babel}% pour avoir \addto
 \usepackage{amsmath}
 \newcommand*\eaddto[2]{% version développée de \addto
    \edef\tmp{#2}%
    \expandafter\addto
    \expandafter#1%
    \expandafter{\tmp}%
 } 


 \newcommand{\essai}{}

    \foreach \x in {1,...,2}   
                    {\eaddto\essai{ 0.5  \symbol{92} \symbol{92} }}  
        \foreach \x in {1,...,3}   
                          {\eaddto\essai{ 1.5  \symbol{92} \symbol{92} }}    
      \foreach \x in {1,...,4}   
                              {\eaddto\essai{ 2.5  \symbol{92} \symbol{92} }}                             
      \foreach \x in {1,...,5}   
               {\eaddto\essai{ 3.5  \symbol{92} \symbol{92} }}
               \begin{align*}
               \essai
               \end{align*}

            \foreach \x in {1,...,2}   
                        {\eaddto\essai{ 4.5  \symbol{92} \symbol{92} }}
                        \begin{align*}
                        \essai  
           \end{align*} 

so, \essai gives " 0.5 \ 0.5 \ 1.5 \1.5 \1.5 ....." but I don't know how to use it in boxplot context

1
  • Welcome to LaTeX! Welcome to TeX.SX!
    – Moriambar
    May 22, 2017 at 20:47

1 Answer 1

1

i found a way to have what i need : put data in one list , because boxplot manual explain : it take one dimension list.

    \documentclass[a4paper,french,svgnames,pdf]{article}
\usepackage[utf8x]{inputenc}  
\usepackage[frenchb]{babel}
\usepackage[np]{numprint}
\usepackage{pgf,tikz}
\usepackage{fp}
\usetikzlibrary{fpu}
\usepgflibrary{fpu}
\usepackage{colortbl}
%\usepackage{xcolor}
%\usepackage{units}
\usepackage{ifthen}
\usepackage{makecell}


% % % % % % % % % stats
\usepackage{pgfplots} % Use tikzpicture for the diagrams
\pgfplotsset{compat=1.12}% Unbedingt unmittelbar nach dem Laden von 
                         % eines aktuellen pgfplots zu bevorzugen!
\usepgfplotslibrary{statistics}% pgf-Libraries besser so laden
\usepackage{pgfplotstable}
 \usetikzlibrary{pgfplots.statistics}

% % % % % % % % set comma for french decimal separator
\pgfset{/pgf/number format/dec sep={,}}
 \pgfkeys{/pgf/number format/.cd, set decimal separator={,},set thousands separator={\ }}   


%*****************************************************************
\FPseed=1234
%\FPseed=1237 % TRÈS IMPORTANT pour compilations multiples avec le même sujet d'initialiser toujours le même nombre aléatoire.
\pgfmathsetseed{2056}
%***********************************

 \begin{document}

    \FPeval\tempsA{trunc(2+random*2,0)}
  \FPeval\tempsB{trunc(3+random*2,0)}
   \FPeval\tempsC{trunc(4+random*3,0)}
    \FPeval\tempsD{trunc(5+random*3,0)}
     \FPeval\tempsE{trunc(4+random*2,0)}
      \FPeval\tempsF{trunc(2+random*2,0)}
       \FPeval\tempsG{trunc(1+random*2,0)}
           \FPeval\sommeSTAT{trunc(\tempsA+\tempsB+\tempsC+\tempsD+\tempsE+\tempsF+\tempsG,0)}

  \par 
    \normalsize  

 \begin{tabular}{|c|c|c|c|c|c|c|c|c|}\hline Durée t (en h)&  $ \mathopen{[} 0 ;  1\mathopen{[} $ &
                   $\mathopen{[} 1 ; 2\mathopen{[} $ & $\mathopen{[} 2;3\mathopen{[} $ & $\mathopen{[} 3; 4\mathopen{[} $ &  $\mathopen{[} 4 ;5\mathopen{[} $ & $\mathopen{[} 5
                   ;6$ & $\mathopen{[} 6 ; 7\mathopen{[} $ & Total---  \\\hline Effectif & \tempsA & \tempsB  & \tempsC  & \tempsD  & \tempsE  & \tempsF
                   & \tempsG  &   \\\hline Centre de classe $x_i$ & 0,5    & 1,5  & 2,5  & 3,5    & 4,5 
                   & 5,5  & 6,5 & \cellcolor{gray}\\\hline

                           \end{tabular}

                                                               \par 

   % % % % % % % % % % % %
%   new table , one for each column  
               \pgfplotstablenew[
                   % define how the ’new’ column shall be filled:
                   create on use/new/.style={create col/expr={0.5}},
                   columns={new}]
                   {\tempsA}
                   \table


               \pgfplotstablenew[
                   % define how the ’new’ column shall be filled:
                   create on use/new/.style={create col/expr={1.5}},
                   columns={new}]
                   {\tempsB}
                   \tableA


               \pgfplotstablenew[
                   % define how the ’new’ column shall be filled:
                   create on use/new/.style={create col/expr={2.5}},
                   columns={new}]
                   {\tempsC}
                   \tableB

               \pgfplotstablenew[
                   % define how the ’new’ column shall be filled:
                   create on use/new/.style={create col/expr={3.5}},
                   columns={new}]
                   {\tempsD}
                   \tableC


               \pgfplotstablenew[
                   % define how the ’new’ column shall be filled:
                   create on use/new/.style={create col/expr={4.5}},
                   columns={new}]
                   {\tempsE}
                   \tableD


               \pgfplotstablenew[
                   % define how the ’new’ column shall be filled:
                   create on use/new/.style={create col/expr={5.5}},
                   columns={new}]
                   {\tempsF}
                   \tableE


               \pgfplotstablenew[
                   % define how the ’new’ column shall be filled:
                   create on use/new/.style={create col/expr={6.5}},
                   columns={new}]
                   {\tempsG}
                   \tableF

% % % % % % %
% % add each column to the first one % % % % %               
                \pgfplotstablevertcat{\table}{\tableA} % loads ‘tableA’ -> ‘\table’

                 \pgfplotstablevertcat{\table}{\tableB} 

                  \pgfplotstablevertcat{\table}{\tableC} 
               \pgfplotstablevertcat{\table}{\tableD} 

               \pgfplotstablevertcat{\table}{\tableE} 

               \pgfplotstablevertcat{\table}{\tableF}


% % % % % % % see \table               
  \pgfplotstabletypeset\table       \par       
                                                                                                       \textbf{Construire } la boite à moustache .

\shorthandoff{;}
% % % % % %  seems uncessary here, but needed in my final .tex in french
\begin{tikzpicture}
    \begin{axis}
        [boxplot/draw direction=x, y=1.5cm,ymax=2,
        boxplot/average=auto, boxplot/every average/.style={mark=},
      %   [y=1.5cm, ymax=2,
         ytick={1,3,...,9}, xtick={0.5,1.5,2.5,3.5,4.5,5.5,6.5}, xticklabels={0.5, 1.5, 2.5,3.5,4.5,5.5,6.5},
         someline/.style={ultra thick,red}
        ]
      \addplot+[boxplot] table[y=new]\table coordinate (A) at (boxplot box cs: \boxplotvalue{average}, 0.5)
      node at
          (boxplot box cs: \boxplotvalue{lower whisker},1.25)
         {\pgfmathprintnumber{\boxplotvalue{lower whisker}}} 
      node at
        (boxplot box cs: \boxplotvalue{lower quartile},1.25)
        {\pgfmathprintnumber{\boxplotvalue{lower quartile}}} 
         node at
            (boxplot box cs: \boxplotvalue{upper quartile},1.25)
           {\pgfmathprintnumber{\boxplotvalue{upper quartile}}}           
           node at
              (boxplot box cs: \boxplotvalue{upper whisker},1)
              {\pgfmathprintnumber{\boxplotvalue{upper whisker}}}
       node[left] at
           (boxplot box cs: \boxplotvalue{median},0.5)         
           {\pgfmathprintnumber{\boxplotvalue{median}}}  
         ;
\end{axis}
\end{tikzpicture}

\strut

\end{document}

so , it's ok in my .tex for auto-multiple-choice and the correction for each student. table with list and boxplot

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .