我希望在曲线图与轴相交的 x 轴上绘制一条小条带。我使用了以下代码
\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns,intersections,calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis x line = middle, axis y line = center, ymin=-3,
xmin=-7, xmax=7,
xtick=\empty, ytick = \empty]
\addplot[smooth, name path=parabola] {x^2-2};
\path[name path=axis] (axis cs:-7,0) -- (axis cs:7,0);
\fill [name intersections={of=parabola and axis,by={a,b}}] (a)
circle (2pt)(b) circle (2pt);
\fill[pattern=north west lines] (a) -- (b) -- ++ (axis direction
cs:0,-0.4) -- cycle;
\fill[pattern=north west lines] (b) -- (a) -- ++ (axis direction
cs:0,-0.4) -- cycle;
\end{axis}
\end{tikzpicture}
\end{document}
我之所以使用两个填充语句,是因为在第一个语句中,我无法弄清楚如何指定从 b 到 a 的负方向的轴方向 cs 矢量。有办法吗?
答案1
您可以简单地使用-|
路径语句返回(a)
:
\documentclass[12pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns,intersections,calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis x line = middle, axis y line = center, ymin=-3,
xmin=-7, xmax=7,
xtick=\empty, ytick = \empty]
\addplot[smooth, name path=parabola] {x^2-2};
\path[name path=axis] (axis cs:-7,0) -- (axis cs:7,0);
\fill [name intersections={of=parabola and axis,by={a,b}}] (a)
circle (2pt)(b) circle (2pt);
\fill[pattern=north west lines] (b) -- ++ (axis direction
cs:0,-0.4) -| (a);
\end{axis}
\end{tikzpicture}
\end{document}