3

Here is the code for a cumulative distribution function of a discrete random variable.

\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]{
            x f(x)
            0 0
            1 1/15
            2 2/15
            3 1/5
            4 4/15
            5 1/3
            6 0
            };
            \end{axis}

The image of the code is like this.enter image description here

However, I think the dot of the CDFenter image description here should be above as shown below.

Can anyone help with the code?

Thank you guys for the advice. It is actually my first time using StackExchange, so might not follow some rules.

The code is from @Jake: tex.stackexchange.com/a/198397/38080. Thank Jake for sharing that.

diana
  • 31
  • 2
    Hi Diana, Welcome to TeX.SX. Please do not post a snippet, but a complete and minimal document. In your case, discontinuos keyword is not in the standard pgfplots --- some additional package is needed. Posting a complete example will help us help you, without having to chasing packages around. – Rmano Jan 15 '21 at 08:26
  • 1
    BTW, the discontinous code seems to come from @Jake: https://tex.stackexchange.com/a/198397/38080 – Rmano Jan 15 '21 at 08:52
  • 1
    @Rmano Thanks for researching the source of the code! – gernot Jan 15 '21 at 13:45

1 Answers1

3

Compared to the code for discontinuous/.style as given in tex.stackexchange.com/a/198397, you have to change the line

\tikzset{options/.style={mark=*,fill=white}}

to

\tikzset{options/.style={mark=*}}

and the line

\draw plot [mark=*] coordinates {(marker-|0,0)};

to

\draw plot [mark=*,mark options={fill=white}] coordinates {(marker-|0,0)};

Which of the two versions is the correct one depends on whether the function is defined using < or <= .

enter image description here

\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.17}

\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=}} \draw [densely dashed] (marker-|0,0) -- (0,0); \draw plot [mark=,mark options={fill=white}] 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=1, xmin=0, xmax=5, 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]{ x f(x) 0 1/15 1 2/15 2 1/5 3 4/15 4 1/3 5 0 }; \end{axis} \end{tikzpicture} \end{document}

gernot
  • 49,614