我正在为学生制作几份讲义,我注意到图表元素的显示顺序可能会分散注意力。这是一张放大的图片,你可以看到我的意思。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];