[Tex/LaTex] plotting the graph of hyperbolic tangent

pgfplotstikz-pgf

Can someone help me to plot the graph of tanh in latex. I know I have to use pgfplot but I do not know actually how to use it. Any type of help is appreciated.

Best Answer

I'd definitely recommend using pgfplots:

enter image description here

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=-2.5, xmax=2.5,
    ymin=-1.5, ymax=1.5,
    axis lines=center,
    axis on top=true,
    domain=-2.5:2.5,
    ylabel=$y$,
    xlabel=$x$,
    ]

    \addplot [mark=none,draw=red,ultra thick] {tanh(\x)};
    \node [right, red] at (axis cs: 1,0.7) {$y = \tanh x$};
    
    %% Add the asymptotes
    \draw [blue, dotted, thick] (axis cs:-2.5,-1)-- (axis cs:0,-1);
    \draw [blue, dotted, thick] (axis cs:+2.5,+1)-- (axis cs:0,+1);
\end{axis}
\end{tikzpicture}
\end{document}
Related Question