0

I wish to add colored dashed vertical lines connecting the orange horizontal lines and also to remove circles on the horizontal lines. My desired output is shown in the picture below:
desired output
Below is my MWE which consists of the code I modified from Drawing cumulative distribution function for a discrete variable.

\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{amsmath}

\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] (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}
    
    
    \pgfplotsset{
        standard/.style={
            axis x line=middle,
            axis y line=middle,
            enlarge x limits=0.05,
            enlarge y limits=0.05,
            every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
            every axis y label/.style={at={(current axis.above origin)},anchor=north east}
        }
    }

    {\centering
        \begin{tikzpicture}
            \begin{axis}[
                standard,
                clip=false,
                jump mark left,
                ymin=0,ymax=1,
                xmin=0, xmax=6,
                xlabel={$t$},
                xtick distance={.5},
                ylabel={$X(t)$},
                yticklabels={,,},
                xtick style={draw=none},
                ytick style={draw=none},
                %axis lines=middle,
                %axis line style={-},
                %ylabel near ticks,
                %xlabel near ticks,
                xticklabels={,,,$t_0$,,$t_1$,,,,$t_2$,$t_3$,,,$t_4$},
                every axis plot/.style={very thick},
                discontinuous,
                table/create on use/cumulative distribution/.style={
                    create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
                }
                ]
                \addplot [orange] table [y=cumulative distribution]{
                    x f(x)
                    1 2/15
                    2 1/5
                    4 4/15
                    4.5 1/3
                    6 0
                };
            \end{axis}
        \end{tikzpicture}
        \par}
\end{document}

which is yielding the following output that I am not desiring:
output from provided MWE

1 Answer 1

3

You could define a reference point that indicates the vertical position at which the vertical dashed lines are to end. In this example it is chosen to be the origin of the axis,

\path (0,0) coordinate (O);

and then change the plot handler such that it draws from the current point all the way (down) to the point that has the current x coordinate and the y coordinate of O,

\draw [densely dashed,blue,thin] (O-|0,0) -- (0,0);

Code:

\documentclass{standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{amsmath}

\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=none}}
                \draw [densely dashed,blue,thin] (O-|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}
    
    
    \pgfplotsset{
        standard/.style={
            axis x line=middle,
            axis y line=middle,
            enlarge x limits=0.05,
            enlarge y limits=0.05,
            every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
            every axis y label/.style={at={(current axis.above origin)},anchor=north east}
        }
    }

    {\centering
        \begin{tikzpicture}
            \begin{axis}[
                standard,
                clip=false,
                jump mark left,
                ymin=0,ymax=1,
                xmin=0, xmax=6,
                xlabel={$t$},
                xtick distance={.5},
                ylabel={$X(t)$},
                yticklabels={,,},
                xtick style={draw=none},
                ytick style={draw=none},
                %axis lines=middle,
                %axis line style={-},
                %ylabel near ticks,
                %xlabel near ticks,
                xticklabels={,,,$t_0$,,$t_1$,,,,$t_2$,$t_3$,,,$t_4$},
                every axis plot/.style={very thick},
                discontinuous,
                table/create on use/cumulative distribution/.style={
                    create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
                }
                ]
                \path (0,0) coordinate (O);
                \addplot [orange] table [y=cumulative distribution]{
                    x f(x)
                    1 2/15
                    2 1/5
                    4 4/15
                    4.5 1/3
                    6 0
                };
            \end{axis}
        \end{tikzpicture}
        \par}
\end{document}

enter image description here

You must log in to answer this question.

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