我又来求助了。我需要用 TikZ 绘制函数 $\frac{1}{x+2}$,突出显示 $x = -2$ 处的垂直渐近线。
我想用黑色虚线画出垂直渐近线,用红色画出函数。我想让函数的所有端点都用虚线画出。我设法写了一个代码,但说实话:它很糟糕。
而且我画的渐近线甚至没有经过 $-2$......
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{scope}[shift={(-10,0)}]
% x & y axis plus scale
\foreach \x in { -3, -2, -1, 1, 2, 3, 4}{
\draw (\x,.1)--(\x,-.1) node[below,fill=white]() {\footnotesize $\x$};
}
\foreach \y in {-6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5}{
\draw (.1,\y)--(-.1,\y) node[left,fill=white]() {\footnotesize $\y$};
}
\draw[->,line width=.5pt, very thick, -{Stealth}] (-5.5,0)-- (5,0) node[right]() {$\mathbf{x}$};
\draw[->,line width=.5pt, very thick, -{Stealth}] (0,-5.5)-- (0,5.3) node[above]() {$\mathbf{y}$};
\node at (2, 2.2) () {$f(x) = \frac{1}{x-2}$};
\node at(.2,-.2) () {\footnotesize $0$};
% Function plot
\begin{scope}
\clip (-1.99,-4) rectangle (4,4);
\draw[red, smooth, domain=-3.6:3.6, line width=1pt] plot (\x, {1/(\x+2)});
\end{scope}
% Clipping for dashed line
\begin{scope}
\clip (0.1,-4.6) rectangle (4,4.5);
% Dashed vertical line at x = -2
\draw[dashed, black, line width=1pt] (-2,-4) -- (-2,4);
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
答案1
解决方案如下tikz
:
代码(根据@Henry代码修改):
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
% x & y axis plus scale
\foreach \x in { -4,-3, -2, -1, 1, 2, 3, 4}{
\draw (\x,.1)--(\x,-.1) node[below,fill=white]() {\footnotesize $\x$};
}
\foreach \y in { -4, -3, -2, -1, 1, 2, 3, 4}{
\draw (.1,\y)--(-.1,\y) node[left,fill=white]() {\footnotesize $\y$};
}
\draw[->,line width=.5pt, very thick, -{Stealth}] (-4,0)-- (4.2,0) node[right]() {$\mathbf{x}$};
\draw[->,line width=.5pt, very thick, -{Stealth}] (0,-4)-- (0,4.2) node[above]() {$\mathbf{y}$};
\node at (2, 2.2) () {$f(x) = \frac{1}{x-2}$};
\node at(.2,-.2) () {\footnotesize $0$};
\clip (-4,-4) rectangle (4,4);
\draw[red, smooth, domain=-4:4, line width=1pt,samples=51] plot (\x, {1/(\x+2)});
\draw[white,line width=3pt,opacity=1] (-2,-5) -- (-2,5);% <<< to cover the red line!
% Dashed vertical line at x = -2
\draw[dashed, black, line width=2pt] (-2,-5) -- (-2,5);
\end{tikzpicture}
\end{document}
答案2
像这样:
代码:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
axis on top,
xmin=-4, xmax=4,
ymin=-5, ymax=5,
no markers,
restrict y to domain=-10:10,
%
xlabel=$x$,
ylabel=$f(x)$
]
\addplot +[thick,samples=101,smooth]{1/(x+2};
\draw[dashed,line width=1pt] (-2,-5)--(-2,5);
\end{axis}
\end{tikzpicture}
\end{document}
添加:pgfplots
将域向左扩展没有问题。
修改后的主体代码:
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xmin=-7.5, xmax=4.5,
ymin=-5, ymax=5,
no markers,
restrict y to domain=-10:10,
%
xlabel=$x$,
ylabel=$y$
]
\addplot +[domain=-7:4,thick,samples=101,smooth]{1/(x+2};
\draw[dashed,line width=1pt] (-2,-5)--(-2,5);
\node[blue,fill=cyan!30] at (2,3) () {$y=\frac{1}{x+2}$};
\draw[fill=black] (0,.5) circle(.05) node[right,inner sep=1pt](a) {};
\draw[latex-](a)--(1,1) node[right] {\tiny $A\left(0,\frac12\right) $};
\end{axis}
\end{tikzpicture}
答案3
- 我认为绘制图表的正确方法在第二个示例中给出,该示例与我们
pgfplots
在第二个答案中的包相同 - 这里有一些小的变化,仅用于练习(代码中的差异已标记):
%\documentclass[12pt]{article}
\documentclass[12pt, margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill, semitransparent, inner sep=2pt},
every pin/.append style = {pin edge={<-,black}, font=\footnotesize, anchor=west,
inner sep=1pt}
]
\begin{axis}[axis lines=middle,
axis on top,
enlargelimits=0.08, % <---
%
xlabel=$x$,
ylabel=$y$,
%
ymin=-6, ymax=6, % <--- changed
restrict y to domain=-10:10,
%
ticklabel style = {rounded corners,
font=\tiny, fill=white, inner sep=1pt}, % <---
xtick = {-6, -5, ..., -1}, % <---
xticklabel style = {anchor=south, yshift=4pt}, % <---
extra x ticks={0,1,...,4}, % <---
extra x tick style={xticklabel style={anchor=north,yshift=-4pt}}, % <---
%
domain=-6.5:4.5, % <--- changed
samples=201, % <--- changed
no markers,
]
\addplot +[thick] {1/(x+2)};
\draw[densely dashed, very thin]
(-2,\pgfkeysvalueof{/pgfplots/ymin}) --
(-2,\pgfkeysvalueof{/pgfplots/ymax}); % <--- changed
\node[dot, pin=above right:{$\bigl(0,\frac{1}{2}\bigr)$}] at (0,{1/(0+2)}) {}; % <--- changed
\node[fill=blue!30, right, font=\footnotesize] at (1,4) {$y=\frac{1}{x+2}$};
\end{axis}
\end{tikzpicture}
\end{document}