# Study of a function with TikZ/pgfplots

Can someone teach me how to plot this in LaTeX with TikZ?

I figured out the code I need:

     \begin{tikzpicture}
\begin{axis}[no marks,
unbounded coords=jump,
axis lines=middle,
xtick = \empty,
ytick = \empty,
xlabel = {$x$},
ylabel = {$y$},
xmajorgrids,]
%PLOT OF THE FUNCTION--------------------------
\addplot+[samples=101,thick,red] {1/(x^2-4)};
%ASYMPTOTES----------------------------------------
\addplot +[mark=none,style=dashed,color=black] coordinates {(2, -2.5) (2, 2.5)};
\addplot +[mark=none,style=dashed,color=black] coordinates {(-2, -2.5) (-2, 2.5)};
%DASHED SPACES------------------------------
\path [pattern=north west lines, pattern color = gray]   (-5,-5) rectangle (-2,0);
\path [pattern=north west lines, pattern color = gray]   (+5,-5) rectangle (+2,0);
\path [pattern=north west lines, pattern color = gray]   (2,4) rectangle (-2,0);
%POINTS---------------------
\draw[fill=black] (0,-1/4) circle (0.05cm);
\node at (1/3,-1/2) {$A$};
\end{axis}
\end{tikzpicture}


This is the output:

Now:

1) how put the xlabel down the axe?

2) Is there a way to have the point of intersection with axis automatically?

3) How ho have +2 and -2 in the dashed areas?

• I am not getting the output pictured above (with the shading) when I run the code. Could you check your code?
– Dan
Nov 8 '16 at 16:07

## 2 Answers

You have to hit the asymptotes for pgfplots to jump. The rest is just the function definition. The rest is given in your other question

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[no marks,unbounded coords=jump,
axis lines=middle,ytick=\empty,xtick={-2,2},xmajorgrids]
\addplot+[samples=101] {1/(x^2-4)};
\end{axis}
\end{tikzpicture}
\end{document}


• See edit please. Nov 8 '16 at 9:28
• @AndreaLeo Please don't change questions along the way. You can always ask a new question such that the issue are contained. The questions you ask are all answered on the site and you can find them with a single search. Nov 8 '16 at 9:31

Adding onto Percusse's answer, to add shading, equation label, and dotted asymptotes you can do the following.

Code:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\begin{tikzpicture}
\begin{axis}[no marks,unbounded coords=jump,
axis lines=middle,ytick=\empty,xtick={-2,2},xmajorgrids]
\addplot+[samples=101] {1/(x^2-4)};
\addplot +[mark=none,style=dashed,color=black] coordinates {(2, -2.5) (2, 2.5)};
\addplot +[mark=none,style=dashed,color=black] coordinates {(-2, -2.5) (-2, 2.5)};
\filldraw[fill=black, draw=black,opacity=.2,] (-5,-5) rectangle (-2,0);
\filldraw[fill=black, draw=black,opacity=.2,] (5,-5) rectangle (2,0);
\filldraw[fill=black, draw=black,opacity=.2,] (-2,0) rectangle (2,4);
\node[text width=3cm] at (4.5,1)
{\small{$f(x)=\frac{1}{x^2-4}$}};
\end{axis}
\end{tikzpicture}
\end{document}


This yields:

• you can instead change the grid line style major x grid style={dashed} Nov 7 '16 at 23:58
• See edits please. Nov 8 '16 at 9:27