4

I want to plot a function that looks something like this

enter image description here

Here is how I am currently doing it

\begin{tikzpicture}
\begin{axis}
\addplot[domain=0:1]{0.27};
\addplot[domain=1:2]{0.62};
\addplot[domain=2:3]{0.82};
\addplot[domain=3:4]{1};
\addplot[only marks,fill=white]coordinates{(0,0.27)(1,0.62)(2,0.82)(3,1)};
\addplot[only marks]coordinates{(0,0.05)(1,0.27)(2,0.62)(3,0.82)(4,1)};
\end{axis}
\end{tikzpicture}

However, I would like to plot each segment using its endpoints, like

\begin{tikzpicture}
\begin{axis}
\addplot[mark=*]coordinates{(0,0)};
\addplot[domain=0:1,mark=*]coordinates{(0,0.27)(1,0.27)};
\addplot[domain=1:2,mark=*]coordinates{(1,0.62)(2,0.62)};
\addplot[domain=2:3,mark=*]coordinates{(2,0.82)(3,0.82)};
\addplot[domain=3:4,mark=*]coordinates{(3,1)(4,1)};
\end{axis}
\end{tikzpicture}

Which looks like

enter image description here

How do you set the markers individually so that the bottom plot looks like the top plot?

1 Answers1

4

Welcome! Applying minimal surgery to the discontinuous style from this answer to be able to optionally switch on the vertical dashed lines yields

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\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}}
                \path [vert] (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}}
    },/tikz/vert/.style={draw=none} %<- change to {draw,densely dashed} if needed
}

\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[jump mark right,discontinuous] coordinates{(0,0) (0,0.27)(1,0.27)(1,0.62)(2,0.62)
(2,0.82)(3,0.82)(3,1)(4,1)};
\end{axis}
\draw[mark=*] plot coordinates {(marker)};
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Thanks! I was hoping for something simpler, but I guess that will do. –  Feb 08 '20 at 22:08