代码
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{width=10cm,compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[
declare function = {
func1(\x)= (\x<=0.3) * (10*\x*\x - 3*\x ) + (\x>0.3) * (\x* \x*\x);
func2(\x)= (\x<=0.3) * (0) + (\x>0.3) * (\x* \x*\x);
}
]
\begin{axis}[
ymin=-0.4,
ymax=1,
xmin=0,
xmax=1,
ytick={-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1},
grid
]
\addplot[name path=poly, blue, domain=-0.4:1, smooth]{func1(x)};
\addplot[name path=poly2, blue, domain=-0.4:1, smooth]{func2(x)};
\addplot[name path=linear, black, no markers]{x};
\addplot[name path=line, black, no markers]{0};
\addplot fill between[
of = poly and line, split,
every odd segment/.style={red!10},
every even segment/.style={yellow!10}
];
\addplot[blue!10] fill between[of = poly2 and linear];
\node at (axis cs:0.18,-0.05) [anchor=north east] {A};
\node at (axis cs:0.45,0.2) [anchor=south west] {B};
\node at (axis cs:0.8,0.3) [anchor=south west] {C};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这是因为 x=0.3 附近的硬弯曲和smooth
选项以及函数求值的样本量“较低”。您可以通过向“func”图添加标记来显示这一点,如下例所示。为了解决您的问题,您可以简单地增加samples
函数求值的数量,例如增加到 101。(默认数量samples
为 25。)
% used PGFPlots v1.17
\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{
compat=newest,
width=10cm,
}
\begin{document}
\begin{tikzpicture}[
declare function = {
func1(\x)= (\x<=0.3) * (10*\x*\x - 3*\x ) + (\x>0.3) * (\x* \x*\x);
func2(\x)= (\x<=0.3) * (0) + (\x>0.3) * (\x* \x*\x);
},
]
\begin{axis}[
ymin=-0.4,
ymax=1,
xmin=0,
xmax=1,
ytick={-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1},
grid,
domain=-0.4:1,
% samples=101, % <-- uncomment me to improve the result
]
\addplot [name path=linear,black] {x};
\addplot [name path=line,black] {0};
\addplot [
name path=poly,
blue,
smooth,
mark=o,
] {func1(x)};
\addplot [
name path=poly2,
red,
smooth,
mark=+,
] {func2(x)};
\addplot fill between[
of=poly and line,
split,
every odd segment/.style={red!10},
every even segment/.style={yellow!10}
];
\addplot [blue!10] fill between [of = poly2 and linear];
\node at (axis cs:0.18,-0.05) [anchor=north east] {A};
\node at (axis cs:0.45,0.2) [anchor=south west] {B};
\node at (axis cs:0.8,0.3) [anchor=south west] {C};
\end{axis}
\end{tikzpicture}
\end{document}