尝试
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
restrict y to domain=-30:30, % <---
ymin = -10, ymax = 10,
xmin = -5, xmax = 5,
domain=-5:5,
samples=101, no marks % <---
]
\addplot {x^-1} node[right, pos=0.9]{$f(x)$};
\end{axis}
\end{tikzpicture}