5

I've been working on a graph for a staircase function, but my code is a mess. Is there a way to make it easier to plot this kind of function?

\documentclass[border=1 cm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw[-latex,thick] (-4, 0) -- (5, 0) node[right] {$x$};
  \draw[-latex,thick] (0, -4) -- (0, 4) node[above] {$y$};
  \draw[domain=0:1, smooth, variable=\x] plot ({\x}, {0});
  \draw[domain=1:2, smooth, variable=\x] plot ({\x}, {1});
  \draw[domain=2:3, smooth, variable=\x] plot ({\x}, {2});
  \draw[domain=3:4, smooth, variable=\x] plot ({\x}, {3});
  \draw[domain=0:-1, smooth, variable=\x] plot ({\x}, {-1});
  \draw[domain=-1:-2, smooth, variable=\x] plot ({\x}, {-2});
  \draw[domain=-2:-3, smooth, variable=\x] plot ({\x}, {-3});
  \foreach \x in {-3,-2,-1,1,2,3,4} 
  \draw[shift={(\x,0)},color=black] node[below] {$\x$};
  \foreach \y in {1,2,3} 
  \draw[shift={(0,\y)},color=black] node[left] {$\y$};
  \foreach \y in {-3,-2,-1} 
  \draw[shift={(0,\y)},color=black] node[right] {$\y$};
  \draw[color=black,fill] (-3,-3) circle (1.5pt);
  \draw[color=black,fill] (-2,-2) circle (1.5pt);
  \draw[color=black,fill] (-1,-1) circle (1.5pt);
  \draw[color=black,fill] (0,0) circle (1.5pt);
  \draw[color=black,fill] (3,3) circle (1.5pt);
  \draw[color=black,fill] (2,2) circle (1.5pt);
  \draw[color=black,fill] (1,1) circle (1.5pt);
  \draw[color=black,fill=white] (-2,-3) circle (1.5pt);
  \draw[color=black,fill=white] (-1,-2) circle (1.5pt);
  \draw[color=black,fill=white] (0,-1) circle (1.5pt);
  \draw[color=black,fill=white] (1,0) circle (1.5pt);
  \draw[color=black,fill=white] (2,1) circle (1.5pt);
  \draw[color=black,fill=white] (3,2) circle (1.5pt);
  \draw[color=black,fill=white] (4,3) circle (1.5pt);
\end{tikzpicture}
\end{document}

And this is my result:

enter image description here

3
6

hm, fastest way? No, but a but shorter code if you use loops:

\documentclass[border=3mm]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}
% axis
\draw[-latex] (-3.5, 0) -- (4.5, 0) node[right] {$x$};
\draw[-latex] (0, -3.5) -- (0, 3.5) node[above] {$y$};
\foreach \x in {-3,-2,-1,1,2,3,4}
  \draw (\x,1mm) -- ++ (0,-2mm) node[below] {$\x$};
\foreach \y in {-3,-2,-1,1,2,3}
{
\ifnum\y<0  
    \draw (-1mm,\y) -- ++ (2mm,0) node[right] {$\y$} 
\else   
    \draw (1mm,\y) -- ++ (-2mm,0) node[left]  {$\y$}
\fi;
}
% staircases
\foreach \i in {-3,...,3}
{
  \filldraw[thick]  (\i,\i) circle[radius=0.5mm] -- ++ (1,0);
  \draw[thick, fill=white]  (\i+1,\i) circle[radius=0.5mm];
}
\end{tikzpicture}
\end{document}

enter image description here

Edit: You may use @Symbol1 suggestion and in above MWE replace

\foreach \y in {-3,-2,-1,1,2,3}
{
\ifnum\y<0  
    \draw (-1mm,\y) -- ++ (2mm,0) node[right] {$\y$} 
\else   
    \draw (1mm,\y) -- ++ (-2mm,0) node[left]  {$\y$}
\fi;
}

with shorter code

\foreach \y in {-3,-2,-1,1,2,3}
    \draw (0,\y) node[inner xsep=2mm, anchor=-90+90*sign(\y)] {$\y$} -- + (-1mm,0) -- + (1mm,0);
4
  • node[right] {$\y$} can be replaced with node[anchor = 90 + 90*sign(\y)] {$\y$}. That saves the\ifnum
    – Symbol 1
    Nov 29 '21 at 6:32
  • @Symbol1, interesting idea but for nice placing of tick labels the code is a bit more complex: \draw (0,\y) node[inner xsep=2mm, anchor = -90+90*sign(\y)] {$\y$} -- + (-1mm,0) -- + (1mm,0);
    – Zarko
    Nov 29 '21 at 6:45
  • Changed my mind, \draw({sign(\y)*0.5}, \y)node{$\y$} (-1mm,\y)--(1mm,\y); is also good alongside the interesting xsep.
    – Symbol 1
    Nov 29 '21 at 7:00
  • 1
    @Symbol1, shorter: yes, but better? Labels positioning is wrong, correcting them by ({sign(-\y)*0.5} gives bad looking result :-(
    – Zarko
    Nov 29 '21 at 7:06
4

A suggestion in pgfplots with jump mark left you can make this:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
    
    \pgfplotsset{
        axissytle/.style={
            every axis x label/.style={at={(current axis.right of origin)},anchor=east,xshift=0.5cm},
            every axis y label/.style={at={(current axis.above origin)},anchor=north,yshift=0.5cm}
        }
    }
    
    \begin{tikzpicture}
        \begin{axis}[
            xmin=-5,
            xmax=5,
            ymin=-5,
            ymax=5,
            xtick={-4,...,4},
            ytick={0,1,...,4},
            axis lines=middle,
            label style={font=\tiny},
            tick label style={font=\tiny},
            axissytle,
            xlabel=$x$,
            ylabel=$y$,
            xtick distance={1},
            ytick distance={1},
            extra y ticks={-4,-3,-2,-1},
            extra y tick style={yticklabel style={xshift=0.5ex, anchor=west}}
            ]
            
            \addplot[jump mark left,mark=*,thick,black]coordinates{(-3,-3)(-2,-2)(-1,-1)(0,0)(1,1)(2,2)};
            \addplot[only marks,mark=*,thick,fill=white,draw=black] coordinates{(-2,-3)(-1,-2)(0,-1)(1,0)(2,1)(3,2)};
            \addplot[mark=o,thick]coordinates{(2,2) (3,2)};
        \end{axis}
    \end{tikzpicture}
    
\end{document}

enter image description here

1
  • Now is much better ;-) +1
    – Zarko
    Nov 30 '21 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.