我设法在不使用宏 fillbetween 的情况下为两条曲线限定的区域着色,因为指令\addplot[gray] fill between[of=F and G, soft clip={domain=0:4}];
没有给出正确的绘图。怎么办?
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[
/pgf/declare function={f(\x)= (3.6*\x +2.4)*exp(-0.6*\x) -1.4;},
/pgf/declare function={g(\x)=4*\x^2 -4*\x +1;},
/pgf/declare function={f_1(\x)=-1*f(\x);},
/pgf/declare function={g_1(\x)=-1*g(\x);}]
\begin{axis}[
restrict x to domain=-2:5, xmax=5, xmin=-1,
restrict y to domain=-3:3, ymax=3, ymin=-3,
x=1.25cm,
y=1.25cm,
axis x line =middle,
axis y line =middle,
tickwidth=0.15cm,
axis on top=true,
grid=both,
axis line style =ultra thick,
major tick style=black,
xtick={0,...,4},
ytick={-2,...,2},
extra x ticks={-1,5},
extra x tick label={\null},
extra y ticks={-3,3},
extra y tick label={\null},
extra tick style={tick style={draw=none}},
y tick label style={
/pgf/number format/1000 sep={}
},
xlabel=$x$,ylabel=$y$,
every axis x label/.style={
at={(ticklabel* cs:1.01)},
anchor=west,
},
every axis y label/.style={
at={(ticklabel* cs:1.01)},
anchor=south,
},
samples=2000,
>=stealth,
]
\addplot [draw=none, fill=gray, domain=0:4] {f(x)}\closedcycle;
\addplot [draw=none, fill=white, domain=0:0.5] {g(x)}\closedcycle;
\addplot [draw=none, fill=gray, domain=0:4] {f_1(x)}\closedcycle;
\addplot [draw=none, fill=white, domain=0:0.5] {g_1(x)}\closedcycle;
\addplot [ultra thick,domain=0:4,smooth] {f(x)};
\addplot [ultra thick,domain=0:0.5,smooth] {g(x)};
\addplot [ultra thick,domain=0:4,smooth,dashed] {f_1(x)};
\addplot [ultra thick,domain=0:0.5,smooth,dashed] {g_1(x)};
\draw[thick](4,{f(4)})--(4,{f_1(4)});
\node [below left] at (0,0) {O};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
这是一个非常简单的方法:不是手动将两个函数拼凑在一起,而是将它们拼凑在参数图中,这样就没有问题了。这种方法的另一个优点是线连接看起来不错。(在您的示例中,线连接被粗轴覆盖,但一般来说这是一个问题。)
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[
/pgf/declare function={f(\x)= (3.6*\x +2.4)*exp(-0.6*\x) -1.4;},
/pgf/declare function={g(\x)=4*\x^2 -4*\x +1;},
/pgf/declare function={f_1(\x)=-1*f(\x);},
/pgf/declare function={g_1(\x)=-1*g(\x);},
/pgf/declare function={F(\x)=ifthenelse(\x<4,f(\x),g(\x-4));},]
\begin{axis}[
restrict x to domain=-2:5, xmax=5, xmin=-1,
restrict y to domain=-3:3, ymax=3, ymin=-3,
x=1.25cm,
y=1.25cm,
axis x line =middle,
axis y line =middle,
tickwidth=0.15cm,
axis on top=true,
grid=both,
axis line style =ultra thick,
major tick style=black,
xtick={0,...,4},
ytick={-2,...,2},
extra x ticks={-1,5},
extra x tick label={\null},
extra y ticks={-3,3},
extra y tick label={\null},
extra tick style={tick style={draw=none}},
y tick label style={
/pgf/number format/1000 sep={}
},
xlabel=$x$,ylabel=$y$,
every axis x label/.style={
at={(ticklabel* cs:1.01)},
anchor=west,
},
every axis y label/.style={
at={(ticklabel* cs:1.01)},
anchor=south,
},
samples=2000,
>=stealth,
]
\addplot [name path=f,ultra thick,domain=-0.5:4,smooth] ({abs(x)},
{ifthenelse(x<0,g(-x),f(x))});
\addplot [name path=f1,ultra thick,domain=-0.5:4,smooth,dashed] ({abs(x)},
{ifthenelse(x<0,g_1(-x),f_1(x))});
\addplot[gray] fill between[of=f and f1];
\draw[thick](4,{f(4)})--(4,{f_1(4)});
\node [below left] at (0,0) {O};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
本着完全公开的精神,我必须说,当你从 更改soft clip
为{domain=0.01:0.5}
时,{domain=0.0:0.5}
解决方案就会停止工作,我实际上不明白原因
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}[
/pgf/declare function={f(\x)= (3.6*\x +2.4)*exp(-0.6*\x) -1.4;},
/pgf/declare function={g(\x)=4*\x^2 -4*\x +1;},
/pgf/declare function={f_1(\x)=-1*f(\x);},
/pgf/declare function={g_1(\x)=-1*g(\x);}]
\begin{axis}[
restrict x to domain=-2:5, xmax=5, xmin=-1,
restrict y to domain=-3:3, ymax=3, ymin=-3,
x=1.25cm,
y=1.25cm,
axis x line =middle,
axis y line =middle,
tickwidth=0.15cm,
axis on top=true,
grid=both,
axis line style =ultra thick,
major tick style=black,
xtick={0,...,4},
ytick={-2,...,2},
extra x ticks={-1,5},
extra x tick label={\null},
extra y ticks={-3,3},
extra y tick label={\null},
extra tick style={tick style={draw=none}},
y tick label style={
/pgf/number format/1000 sep={}
},
xlabel=$x$,ylabel=$y$,
every axis x label/.style={
at={(ticklabel* cs:1.01)},
anchor=west,
},
every axis y label/.style={
at={(ticklabel* cs:1.01)},
anchor=south,
},
samples=200,
>=stealth,
]
\addplot [name path=F, ultra thick,domain=0:4,smooth] {f(x)};
\addplot [name path=G, ultra thick,domain=0:0.5,smooth] {g(x)};
\addplot [name path=F1, ultra thick,domain=0:4.0,smooth,dashed] {f_1(x)};
\addplot [name path=G1, ultra thick,domain=0:0.5,smooth,dashed] {g_1(x)};
\addplot[gray] fill between[of=F1 and G1, soft clip={domain=0.01:0.5}];
\addplot[gray] fill between[of=F and G, soft clip={domain=0.01:0.5}];
\addplot[gray] fill between[of=F1 and F, soft clip={domain=0.5:4.0}];
\end{axis}
\end{tikzpicture}
\end{document}