我想将 x 轴上的刻度标记 -2 从其通常位置移到 x 轴上方。我绘制了一条从 (-2,0) 到 (-2,-4) 的虚线,但我不想让它超出这个刻度标记。
\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=4in,axis equal image,
axis lines=middle,
xmin=-16,xmax=16,samples=201,
xlabel=$x$,ylabel=$y$,
ymin=-12,ymax=12,
restrict y to domain=-12:12,
enlargelimits={abs=1cm},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xtick={-2},ytick={\empty},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot [latex-latex,domain=-10:14] {x - 2} node [pos=0.9, anchor=north west, font=\footnotesize] {$y=\frac{x^{2}-4}{x+2}$};
\draw [thin,dashed] (-2,0) -- (-2,-4);
\draw [fill=white] (-2,-4) circle [radius=1.5pt] node[left,font=\tiny]{$(-2, \, -4)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
放置单身的勾选标签是通过\node
:
笔记:
- 如果访问环境内的坐标,
axis
则应该指定axis cs
坐标系。
代码:
\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=4in,axis equal image,
axis lines=middle,
xmin=-16,xmax=16,samples=201,
xlabel=$x$,ylabel=$y$,
ymin=-12,ymax=12,
restrict y to domain=-12:12,
enlargelimits={abs=1cm},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xtick={\empty},%xtick={-2},
ytick={\empty},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot [latex-latex,domain=-10:14] {x - 2} node [pos=0.9, anchor=north west, font=\footnotesize] {$y=\frac{x^{2}-4}{x+2}$};
\draw [thin,dashed] (axis cs: -2,-4) -- (axis cs: -2,0) node [above] {\tiny$-2$};
\draw [fill=white] (axis cs: -2,-4) circle [radius=1.5pt] node[left,font=\tiny]{$(-2, \, -4)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
另一种方法是使用xticklabel shift
密钥:
\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=4in,axis equal image,
axis lines=middle,
xmin=-16,xmax=16,samples=201,
xlabel=$x$,ylabel=$y$,
ymin=-12,ymax=12,
restrict y to domain=-12:12,
enlargelimits={abs=1cm},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xtick={-2},ytick={\empty},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west},
xticklabel shift=-16pt,
]
\addplot [latex-latex,domain=-10:14] {x - 2} node [pos=0.9, anchor=north west, font=\footnotesize] {$y=\frac{x^{2}-4}{x+2}$};
\draw [thin,dashed] (-2,0) -- (-2,-4);
\draw [fill=white] (-2,-4) circle [radius=1.5pt] node[left,font=\tiny]{$(-2, \, -4)$};
\end{axis}
\end{tikzpicture}
\end{document}
请注意,这将转移全部x 如果您的实际用例有多个,请勾选标签。
答案3
这个例子并没有准确地回答问题,但却是一个给问题添加色彩的例子:
\documentclass{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{color}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=4in,axis equal image,
axis lines=middle,
xmin=-16,xmax=16,samples=201,
xlabel=${\color{blue}{x}}$,
ylabel=${\color{blue}{y}}$,
ymin=-12,ymax=12,
restrict y to domain=-12:12,
enlargelimits={abs=1cm},
axis line style={latex-latex},
ticklabel style={font=\tiny,fill=white},
xtick={\empty},%xtick={-2},
ytick={\empty},
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot [purple, latex-latex,domain=-10:14] {x - 2} node [pos=0.9, anchor=north west, font=\footnotesize]
{\color{violet}{$y=\frac{x^{2}-4}{x+2}$}};
\draw [blue,thin,dashed] (axis cs: -2,-4) -- (axis cs: -2,0.2) node [above] {\tiny$-2$};
\draw [green, thin, dashed] (axis cs: -2, -4) -- (axis cs: 0.2, -4) node [right] {\tiny$-4$};
\draw [red, fill=white] (axis cs: -2,-4) circle [radius=1.5pt] node[left,font=\tiny]{$(-2, \, -4)$};
\end{axis}
\end{tikzpicture}
\end{document}