y=-x^2, y=x^2+1
我正在尝试为抛物线和线条所包围的区域进行着色x=-1, x=3
。
通过在这里使用@Salim Bou 的答案pgfplots 使用多条曲线填充,我使用了以下代码:
\begin{figure}[h]
\begin{tikzpicture}
\begin{axis}
[xlabel=$x$,ylabel=$y$,axis lines=middle,xtick={30},ytick={30},no marks,axis equal,xmin=-2,xmax=4,ymin=-10,ymax=10,enlargelimits={upper=0.1}]
\addplot[no markers,thick,samples=1001, samples y=0,domain=-1:3,variable=x, name path=A]({ x },{ -x^2 });
\addplot[no markers,thick,samples=1001, samples y=0,domain=-1:3,variable=x, name path=B]({ x },{ x^2+1 });
\addplot[no markers,thick,samples=3, samples y=0,domain=-1:2,variable=t]({ -1 },{ t });
\addplot[no markers,thick,samples=3, samples y=0,domain=-9:10,variable=t]({ 3 },{ t });
\addplot[gray!30,opacity=0.6] fill between[of=A and B, soft clip={domain=-1:3}];
\node[above right] at (0,0) {\scalebox{0.5}{$O$}};
\node[below left] at (-1,0) {\scalebox{0.5}{$-1$}};
\node[below right] at (3,0) {\scalebox{0.5}{$3$}};
\end{axis}
\end{tikzpicture}
\end{figure}
但在我的示例中该命令似乎\addplot[gray!30,opacity=0.6] fill between[of=A and B, soft clip={domain=-1:3}]
没有任何效果。
我怎样才能解决这个问题 ?
答案1
在等待错误修复时,您可以使用pgfplots
无soft clip
宏:
\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections, positioning}
\begin{document}
\begin{tikzpicture}[node distance=0pt,
N/.style={font=\tiny, inner sep=1pt}
]
\begin{axis}[
axis lines=middle,
axis equal,
xlabel=$x$, ylabel=$y$,
xtick=\empty, ytick=\empty,
xmin=-2, xmax=4,
ymin=-10, ymax=10,
no marks,
samples=101,
domain=-1:3
]
\addplot[thick,name path=A] {-x^2};
\addplot[thick,name path=B] { x^2+1};
\draw[thick] (axis cs:-1,-1) -- (axis cs:-1,2);
\draw[thick] (axis cs: 3,-9) -- (axis cs:3,10);
\addplot[gray!30,opacity=0.6] fill between[of=A and B];
\node[N, above right=of {axis cs: 0,0}] {$ 0$};
\node[N, below left=of {axis cs:-1,0}] {$-1$};
\node[N, below right=of {axis cs: 3,0}] {$ 3$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
在等待错误修复的同时,以下是元帖子,你可以用它编译lualatex
(或改编为普通 MP,或GMP包装)。
\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
numeric u, s;
u = 5mm;
path xx, yy, pp, A, B, S;
xx = (left--right) scaled 8u;
yy = xx rotated 90;
s = 1/8;
pp = ( (-1,1) for x = s-1 step s until 3: -- (x,x**2) endfor ) scaled u;
A = pp shifted (0,u);
B = reverse pp reflectedabout(left, right);
S = A -- B -- cycle;
fill S withcolor 3/4 white;
drawarrow xx withcolor 1/4 white;
drawarrow yy withcolor 1/4 white;
draw S;
label.rt("$x$", point 1 of xx);
label.top("$y$", point 1 of yy);
label.llft("$-1$", (-u,0));
label.lrt("$3$", (3u,0));
endfig;
\end{mplibcode}
\end{document}