# Plotting the sign function

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},
},
]
\addplot[empty point] coordinates {(0, -1) (0, 1)};
\end{axis}
\end{tikzpicture}
\end{document}


Another approach. I have borrowed the code from Heiko Oberdiek's solution and defined the sign(x) function, which can be used as follows:

\addplot {sign(x)};


Complete example:

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

\tikzset{
declare function={
sign(\x) = (and(\x<0, 1) * -1) +
(and(\x>0, 1) * 1);
}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xlabel=$x$,
ylabel={$y$},
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},
},
]
mark=*,
mark options={fill=white, draw=black},
only marks,
] coordinates {(0, -1) (0, 1)};
domain=0.001:\pgfkeysvalueof{/pgfplots/xmax}]
{sign(x)};
domain=\pgfkeysvalueof{/pgfplots/xmin}:-0.001]
{sign(x)};
\end{axis}
\end{tikzpicture}
\end{document}


Result:

• FYI, pgf/TikZ 3.0.1 contains function sign, see feature request 81. Sep 13 '15 at 10:17
• @HeikoOberdiek Thanks. Is it supposed to work in \addplot {}? Sep 13 '15 at 10:40
• Yes, it's part of pgf/TikZ. Sep 13 '15 at 10:42
• Either use the high level function as in your answer, or you have to provide low level definitions for the various math engines, especially fpu, which is used by pgfplots. The link to the feature request only meant, that a definition is not needed at all with recent pgf/TikZ version, sign can be used directly. Sep 13 '15 at 10:56
• Like so \addplot+[no marks,samples=201] {sign(x)}; Sep 13 '15 at 16:40

and another way....

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[axis lines=middle,xlabel={$x$},ylabel={sgn$(x)$},enlargelimits=true]