![如何在 Tikz 中填充由两个抛物线围成的区域?](https://linux22.com/image/409776/%E5%A6%82%E4%BD%95%E5%9C%A8%20Tikz%20%E4%B8%AD%E5%A1%AB%E5%85%85%E7%94%B1%E4%B8%A4%E4%B8%AA%E6%8A%9B%E7%89%A9%E7%BA%BF%E5%9B%B4%E6%88%90%E7%9A%84%E5%8C%BA%E5%9F%9F%EF%BC%9F.png)
如何使用 Tikz 在两个抛物线之间填充?我尝试使用以下代码。但我无法填充该区域。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:2]
%\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$y$};
\draw[color=red] plot (\x,\x^2) node[right] {$y =x^2$};
\draw[color=blue] plot (\x,\x^2/4) node[right] {$y =x^2/4$};
\end{tikzpicture}
\end{document}
它产生:
答案1
像这样?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[domain=0:2]
%\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$y$};
\fill[cyan] plot (\x,\x^2) -- plot[domain=2:0] (\x,\x^2/4);
\draw[color=red] plot (\x,\x^2) node[right] {$y =x^2$};
\draw[color=blue] plot (\x,\x^2/4) node[right] {$y =x^2/4$};
\end{tikzpicture}
\end{document}
或者您也可以使用pgfplots
。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:2,axis lines=middle,xlabel=$x$,ylabel=$y$,clip=false,
xtick=\empty,ytick=\empty,unit vector ratio=1 1 1]
\addplot[color=red,thick,name path=A] {x^2} node[right] {$y =x^2$};
\addplot[color=blue,thick,name path=B] {x^2/4} node[right] {$y =x^2/4$};
\addplot[cyan] fill between [of=A and B];
\end{axis}
\end{tikzpicture}
\end{document}