# [Tex/LaTex] Plotting the sign function

pgfplotsplot

I'd like to create a graph of the sign function in my document: using pdfplots and tizkpicture (as used here). Could you please let me know how to define the function in the addplot part?

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel={$\sgn(x)$}
]
\end{axis}
\end{tikzpicture}
\end{document}


The following example solves the function drawing by dividing it into parts:

• Line at y=-1 from left to the y axis at x=0.
• Line at y=1 from the y axis at x=0 to the right.
• Red dot at point (0, 0).
• White dot with black circle to exclude points at (0, -1) and (-1, 0).

The minus sign of y tick "-1" would be overprinted by the red line. Therefore, the tick is set as extra tick with a style that moves the tick mark to the right.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\DeclareMathOperator{\sgn}{sgn}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel=$x$,
ylabel={$\sgn(x)$},
xmin=-3, xmax=3,
ymin=-1.5, ymax=1.5,
xtick=\empty,
ytick={0, 1},
extra y ticks={-1},
extra y tick style={
tick label style={anchor=west, xshift=3pt},
},
function line/.style={
red,
thick,
samples=2,
},
single dot/.style={
red,
mark=*,
},
empty point/.style={
only marks,
mark=*,
mark options={fill=white, draw=black},
},
] 