[Tex/LaTex] How to plot a Cumulative Distribution Function (CDF) of Discrete Numbers in LaTex Environment

pgfplotsplot

The overall achievement vector of a person/household is designated by x=(x_1 \cdots x_n). To demonstrate, imagine that there are five persons in a society with over all income $25, $18, $15, $33, $31. In this case x=(25, 18, 15, 33, 31) is a vector indicating the income of the society.
As can be seen, ordering/ranking the overall achievement vector; in other words, ranking/ordering individuals or households by their achievements yields (15, 18, 25, 31, 33).
The aim here is to plot the cumulative distribution function for X? It is an excerpt my research Thesis – a Welfare Analysis approach in the Multidimensional Poverty Analysis domains!

\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1,
    xmin=0, xmax=6,
    every axis plot/.style={very thick},
    discontinuous,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
\addplot [red] table [y=cumulative distribution]{
P(x) f(x)
<15  0
15  1/5
18  2/5
25 3/5
31 4/5
33 1
};
\end{axis}

Best Answer

I have adjusted the following things to obtain the result below:

  • The code for the key discontinuous is copied from here.

  • The x and y ranges didn't fit; I adjusted them to

    ymin=0,ymax=3.5, xmin=14,xmax=35,
    
  • Currently the first column is taken as the x values, hence "<15" doesn't work. I replaced it by 14, the smallest value of the range.

  • I added the upper bound of the x range (35) to the table, such that the last accumulated value is also drawn.

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}

\pgfplotsset{
    discontinuous/.style={
    scatter,
    scatter/@pre marker code/.code={
        \ifnodedefined{marker}{
            \pgfpointdiff{\pgfpointanchor{marker}{center}}%
             {\pgfpoint{0}{0}}%
             \ifdim\pgf@y>0pt
                \tikzset{options/.style={mark=*, fill=white}}
                \draw [densely dashed,blue] (marker-|0,0) -- (0,0);
                \draw plot [mark=*] coordinates {(marker-|0,0)};
             \else
                \tikzset{options/.style={mark=none}}
             \fi
        }{
            \tikzset{options/.style={mark=none}}        
        }
        \coordinate (marker) at (0,0);
        \begin{scope}[options]
    },
    scatter/@post marker code/.code={\end{scope}}
    }
}

\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=3.5,
    xmin=14,xmax=35,
    xlabel={income},
    ylabel={cumulative distribution},
    every axis plot/.style={very thick},
    discontinuous,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
\addplot [red] table [y=cumulative distribution]{
P(x) f(x)
14   0
15  1/5
18  2/5
25 3/5
31 4/5
33 1
35 0
};
\end{axis}
\end{tikzpicture}
\end{document}