TransWikia.com

How to change the dot of discontinuous latex plot?

TeX - LaTeX Asked on May 2, 2021

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.

One Answer

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
longdefifnodedefined#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}}%
             ifdimpgf@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}

Answered by gernot on May 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP