
我一直在嘗試找到一種方法來對曲線之間的區域進行著色,但是當我嘗試使用 Softclip 在 LaTeX 中進行編碼時,一切都會關閉。我不知道該怎麼辦。
這是我的程式碼:
\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
axis lines=middle,
xmin=-2, xmax=5,
ymin=-5, ymax=10,
xlabel={$x$},
ylabel={$y$},
legend style={at={(0.5,-0.15)}, anchor=north},
]
\addplot[blue, domain=-2:5, samples=100, name path=A] {x^2 - 2*x};
\addlegendentry{$y = x^2 - 2x$}
\addplot[red, domain=-2:5, samples=100, name path=B] {4*x - x^2};
\addlegendentry{$y = 4x - x^2$}
% Create a path for the shaded region
\path[name path=zero] (0,0) -- (0,8);
% Shade the region between the two functions in red
\addplot[red, fill, opacity=0.3] fill between[of=A and B, soft clip={domain=0:3}] \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
答案1
當我嘗試運行您的程式碼時,我看到以下訊息:
! Package pgfplots Error: 'name path' is undefined. Please load \usetikzlibrary {intersections} or \usepgfplotslibrary{fillbetween}.
所以我就這麼做了。我\usepgfplotslibrary{fillbetween}
在序言中插入了。現在它似乎對我來說非常有效。
\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
axis lines=middle,
xmin=-2, xmax=5,
ymin=-5, ymax=10,
xlabel={$x$},
ylabel={$y$},
legend style={at={(0.5,-0.15)}, anchor=north},
]
\addplot[blue, domain=-2:5, samples=100, name path=A] {x^2 - 2*x};
\addlegendentry{$y = x^2 - 2x$}
\addplot[red, domain=-2:5, samples=100, name path=B] {4*x - x^2};
\addlegendentry{$y = 4x - x^2$}
% Create a path for the shaded region
\path[name path=zero] (0,0) -- (0,8);
% Shade the region between the two functions in red
\addplot[red, fill, opacity=0.3] fill between[of=A and B, soft clip={domain=0:3}] \closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
只要這是你想要的...