我想对朝上的抛物线进行阴影处理,但只处理两者不相交的区域,即 (0,-2) 到 (0,2) 的区域。
\documentclass[fleqn]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,snakes}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
samples=1000,
xlabel=$x$,
ylabel=$y$,
xmin=-4.5,xmax=4.5,
ymin=-4.5,ymax=4.5,
grid=both,
grid style={line width=.01pt, draw=black!25},
major grid style={line width=.1pt},
axis lines=middle,
x label style={at={(current axis.right of origin)},anchor=north, below=-5mm},
minor x tick num=3,
minor y tick num=3,
ticklabel style={font=\small},
enlargelimits={abs=0.5},
]
\addplot[smooth,domain = -6:6,name path = A]{x^2-2};
\addplot[smooth,domain = -6:6,name path = B]{2-x^2};
\addplot[draw=none,name path = C]{6};
\addplot[gray,opacity=0.2] fill between[of=A and C,soft clip={domain=-10:10}];
\end{axis}
\end{tikzpicture}
\end{document}
这是我目前拥有的代码。
答案1
对于仅对抛物线重叠部分进行阴影处理,您可以使用选项split
和every even segment/.style={...}
:
梅威瑟:
\documentclass[fleqn]{article}
\usepackage{pgfplots} % it load tikz too
\pgfplotsset{compat=1.17} % it is very recommended to be added
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{arrows,
backgrounds,
decorations.pathmorphing,
% snakes is part of decorations.pathmorphing
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
x label style={anchor=north east},
xmin=-4.5, xmax=4.5, xlabel=$x$,
ymin=-4.5, ymax=4.5, ylabel=$y$,
grid=both,
grid style={line width=.1pt},
major grid style={line width=.1pt, black!75},
minor x tick num=3,
minor y tick num=3,
ticklabel style={font=\small},
enlargelimits={abs=0.5},
domain = -6:6, samples=101,
every axis plot post/.append style={thick}
]
\addplot[name path = A]{x^2-2};
\addplot[name path = B]{2-x^2};
\addplot[gray,opacity=0.2]
fill between[of=A and B, split,
every even segment/.style={fill=none}];
\end{axis}
\end{tikzpicture}
\end{document}
正如您所注意到的,在上述 MWE 中,与您的相比,进行了以下更改:
- 删除的是加载
tikz
包,因为它加载了pgfplots
- 添加的是
\pgfplotsset
- 过时的
snake
库被替换为decorations.pathmorphing
- 改变的是
x label style
- 改变的是
grid style
(小网格更加清晰可见) - 改变的是
major grid style
(它更加清晰可见) - 功能固化更厚
- 删除不使用的`\addplot
结果是:
答案2
你是这个意思吗:我对 pgf 图不是很熟悉。我只发现你想绘制图 A 和 C 之间的区域。通过将 C 更改为 B,它会绘制函数 A 和 B 之间的区域。然后你只需调整soft clip={domain=< >}
。
\documentclass[fleqn]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds,snakes}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
samples=1000,
xlabel=$x$,
ylabel=$y$,
xmin=-4.5,xmax=4.5,
ymin=-4.5,ymax=4.5,
grid=both,
grid style={line width=.01pt, draw=black!25},
major grid style={line width=.1pt},
axis lines=middle,
x label style={at={(current axis.right of origin)},anchor=north, below=-5mm},
minor x tick num=3,
minor y tick num=3,
ticklabel style={font=\small},
enlargelimits={abs=0.5},
]
\addplot[smooth,domain = -6:6,name path = A]{x^2-2};
\addplot[smooth,domain = -6:6,name path = B]{2-x^2};
\addplot[draw=none,name path = C]{6};
\addplot[gray,opacity=0.2] fill between[of=A and B,soft clip={domain=-1.5:1.5}];
\end{axis}
\end{tikzpicture}
\end{document}