我不知道如何添加渐近线“x = 1”,其中 x=f(y)。我看到 pgfplots 会自动添加此渐近线,但是我如何将其设置为黑色和虚线样式?
下面是我的代码:
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
xmin = -20,
xmax = 20,
ymin = -20,
ymax = 20,
]
%f(x)
\addplot [
domain=-20:20,
samples=100,
color=red,
]
{(3*x+6)/(x-1)};
\addlegendentry{$f(x)$}
%Vertical Asymptote
%\draw [dashed] (1,20) -- (1,-20);
%\addplot[domain=-20:20,variable=\x,style=dashed] {1};
\draw[domain= 1:1.1,variable=\y,style=dashed] plot ({1},{\x});
%Horizontal Asymptote
\addplot [style=dashed, domain=-20:20]{3};
\end{axis}
\end{tikzpicture}
当前图表如下所示:
答案1
欢迎!该函数有两个特殊点,一个是分子改变符号时,另一个是分母改变符号时。因此将图分成三部分是有意义的。(目前中间部分不可见,但如果您更改不同的值,它可能会变得可见ymax
。我还添加了unbounded coords=jump
。)可以通过添加渐近线\draw
,其中端点通过\pgfkeysvalueof{/pgfplots/ymin}
等确定。(顺便说一句,当您说variable=\y
图中的变量也必须变成时\y
,但我用直线代替了该图。)
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
xmin = -20,
xmax = 20,
ymin = -20,
ymax = 20,
]
%f(x)
\addplot [unbounded coords=jump,
domain=-20:-2,
samples=41,
color=red,
]
{(3*x+6)/(x-1)};
\addlegendentry{$f(x)$}
\addplot [unbounded coords=jump,
domain=-2:1,
samples=16,
color=red,
]
{(3*x+6)/(x-1)};
\addplot [unbounded coords=jump,
domain=1:20,
samples=46,
color=red,
]
{(3*x+6)/(x-1)};
%Vertical Asymptote
%\draw [dashed] (1,20) -- (1,-20);
%\addplot[domain=-20:20,variable=\x,style=dashed] {1};
\draw[dashed] (1,\pgfkeysvalueof{/pgfplots/ymin}) --
(1,\pgfkeysvalueof{/pgfplots/ymax})
(\pgfkeysvalueof{/pgfplots/xmin},3) --
(\pgfkeysvalueof{/pgfplots/xmax},3);
\end{axis}
\end{tikzpicture}
\end{document}