为什么 fillbetween 不起作用

为什么 fillbetween 不起作用

我想填充以下域 [0,3] 中函数 -x^3+4.5x^2-5.5x+3 和 0 之间的区域,但是我收到了错误消息:

Runaway argument?
{}lbetween[of = f and g]; \end {axis} \end {tikzpicture} 
! Paragraph ended before \pgfplots@addplotimpl@fillbetween was complete.

这是我的代码:

\begin{tikzpicture}[scale=0.75]
\begin{axis}
\addplot[domain=0:3,name path=f,samples=50] {-x^3+4.5*x^2-5.5*x+3};
\addplot[line width=3pt,color=red,domain=0:0.21,samples=50] {-x^3+4.5*x^2-5.5*x+3};
\addplot[line width=3pt,color=red,domain=2.15:3,samples=50] {-x^3+4.5*x^2-5.5*x+3};
\draw[dashed] (0.21,2.04) -- (2.15,2.04);
\path[name path=g] (axis cs:0,0) -- (axis cs:3,0);
\addplot[gray!50] fillbetween[of = f and g];
\end{axis}
\end{tikzpicture}

这看起来非常简单,但你做到了。提前致谢。

答案1

该命令是\addplot[gray!50] fill between[of=f and g];,但你不应该用于fillbetween如此简单的区域

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[draw=none, fill=gray!50, domain=0:3, samples=50, forget plot] {-x^3+4.5*x^2-5.5*x+3} \closedcycle;
\addplot[domain=0:3, samples=50] {-x^3+4.5*x^2-5.5*x+3};
\addplot[red, line width=3pt, domain=0:0.21, samples=50] {-x^3+4.5*x^2-5.5*x+3};
\addplot[red, line width=3pt, domain=2.15:3, samples=50] {-x^3+4.5*x^2-5.5*x+3};
\draw[dashed] (0.21,2.04) -- (2.15,2.04);
\end{axis}
\end{tikzpicture}
\end{document}

下方有曲线和填充区域的图形

相关内容