我正在為學生創建幾份講義,我注意到圖表中的元素出現的順序可能會分散注意力。這是一張放大的圖片,您可以明白我的意思。 x=-2 處的圓應該是圖中的一個洞,但它出現在線條下方。這同樣適用於 y=0 處的線和網格本身。似乎元素以與我需要的完全相反的順序呈現。 有沒有辦法控制順序,讓網格在所有東西下面,y=0在軸上面(所以中間沒有黑線),兩個圓圈在所有東西上面?我的這個圖的程式碼如下:
\documentclass[12pt]{extarticle}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{vasymptote/.style={
before end axis/.append code={
\draw[densely dashed, magenta] ({rel axis cs:0,0} -| {axis cs:#1,0})
-- ({rel axis cs:0,1} -| {axis cs:#1,0});
}
}}
\begin{document}
\begin{tikzpicture}
\def\FunctionF(#1){(#1+2)/((#1)^2-4)}
\def\FunctionG(#1){0}
\begin{axis}[
axis y line=center,
axis x line=middle,
axis on top=false,
xmin=-6.5, xmax=6.5,
ymin=-6.5, ymax=6.5,
grid,
xtick={-6,...,6},
ytick={-6,...,6},
yticklabels={-6,,-4,,-2,,,,2,,4,,6},
xticklabels={-6,,-4,,-2,,,,2,,4,,6},
vasymptote=2,
]
% Hole
\coordinate (A) at (-2,-1/4);
% y-intercept
\coordinate (B) at (0,-1/2);
% Hole
\draw[magenta, thick, fill=white] (A) circle (2pt);
% y-intercept
\filldraw[magenta, thick] (B) circle (2pt);
% f(x)
\addplot [domain=-7:2-0.1, samples=50, ultra thick, darkgray] {\FunctionF(x)};
\addplot [domain=2+0.1:7, samples=50, ultra thick, darkgray] {\FunctionF(x)};
\node [right, darkgray, thick] at (axis cs: 3,2) {$f(x)$};
%g(x)
\addplot [domain=-7:7, samples=20, ultra thick, magenta, densely dashed] {\FunctionG(x)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
這是使用圖層的一種方法。在圖層中添加set layers
並繪製圓圈axis foreground
,例如,
\begin{pgfonlayer}{axis foreground}
\draw[magenta, thick, fill=white] (A) circle[radius=2pt];
% y-intercept
\filldraw[magenta, thick] (B) circle [radius=2pt];
\end{pgfonlayer}
完整範例
\documentclass[12pt]{extarticle}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\pgfplotsset{vasymptote/.style={
before end axis/.append code={
\draw[densely dashed, magenta] ({rel axis cs:0,0} -| {axis cs:#1,0})
-- ({rel axis cs:0,1} -| {axis cs:#1,0});
}
}}
\begin{document}
\begin{tikzpicture}
\def\FunctionF(#1){(#1+2)/((#1)^2-4)}
\def\FunctionG(#1){0}
\begin{axis}[
axis y line=center,
axis x line=middle,
axis on top=false,
xmin=-6.5, xmax=6.5,
ymin=-6.5, ymax=6.5,
grid,
xtick={-6,...,6},
ytick={-6,...,6},
yticklabels={-6,,-4,,-2,,,,2,,4,,6},
xticklabels={-6,,-4,,-2,,,,2,,4,,6},
vasymptote=2,
set layers%<- added
]
% Hole
\coordinate (A) at (-2,-1/4);
% y-intercept
\coordinate (B) at (0,-1/2);
% Hole
\begin{pgfonlayer}{axis foreground}
\draw[magenta, thick, fill=white] (A) circle[radius=2pt];
% y-intercept
\filldraw[magenta, thick] (B) circle [radius=2pt];
\end{pgfonlayer}
% f(x)
\addplot [domain=-7:2-0.1, samples=50, ultra thick, darkgray] {\FunctionF(x)};
\addplot [domain=2+0.1:7, samples=50, ultra thick, darkgray] {\FunctionF(x)};
\node [right, darkgray, thick] at (axis cs: 3,2) {$f(x)$};
%g(x)
\addplot [domain=-7:7, samples=20, ultra thick, magenta, densely dashed] {\FunctionG(x)};
\end{axis}
\end{tikzpicture}
\end{document}
您也可以在繪圖後繪製圓圈。
\addplot [domain=-7:2-0.1, samples=50, ultra thick, darkgray] {\FunctionF(x)};
\addplot [domain=2+0.1:7, samples=50, ultra thick, darkgray] {\FunctionF(x)};
\node [right, darkgray, thick] at (axis cs: 3,2) {$f(x)$};
%g(x)
\addplot [domain=-7:7, samples=20, ultra thick, magenta, densely dashed] {\FunctionG(x)};
% Hole
\draw[magenta, thick, fill=white] (A) circle[radius=2pt];
% y-intercept
\filldraw[magenta, thick] (B) circle [radius=2pt];