如何使用 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}