曲线间填充 – pgfplots

曲线间填充 – pgfplots

我已经看到了足够多的类似主题,但经过fill between长时间的调整选项后,我仍然无法在曲线和之间获得正确的填充y=1

这是我目前所能取得的最好成绩。 在此处输入图片描述

以下是代码:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
axis lines = center,
axis line style = thick,
xlabel=$x$, ylabel=$y$,
ylabel style={left},
ymin=-3,
ymax=3.5,
xmin=-4,
xmax=8,
unit vector ratio=1 1,
width=15cm,
xtick=\empty, 
ytick=\empty,
clip=false
]
\addplot [name path=C, draw=none] {0};
\addplot [name path=D, domain=1:3] {1};
\addplot [name path=A,blue, very thick,samples=100, domain=-2:2] ({x^2+2},{x});
\addplot [name path=B,cyan, very thick,samples=100, domain=-1.5:1.5] ({x},{x^3});

\addplot[gray,opacity=0.2] fill between [of=C and D, soft clip={domain=1:2}];

\end{axis}
\end{tikzpicture}

\end{document}

答案1

对此有一个通用的解决方案:将两条路径的交叉段合并为一条新路径,将这条路径与下一条路径合并,然后将这条路径与上一条路径合并。但是,为此,必须始终猜测段号。

这个例子很简单,你可以直接做

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
axis lines = center,
axis line style = thick,
xlabel=$x$, ylabel=$y$,
ylabel style={left},
ymin=-3,
ymax=3.5,
xmin=-4,
xmax=8,
unit vector ratio=1 1,
width=15cm,
xtick=\empty, 
ytick=\empty
]
\fill[gray,opacity=0.2] plot[domain=0:1] (\x,\x*\x*\x) -- plot[domain=1:0] 
({\x*\x+2},{\x});
\addplot [name path=C, draw=none,samples=2] {0};
\addplot [name path=D, domain=1:3,samples=2] {1};
\addplot [name path=A,blue, very thick,samples=100, domain=-2:2] ({x^2+2},{x});
\addplot [name path=B,cyan, very thick,samples=100, domain=-2:2] ({x},{x^3});


\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

以下是通用方法。

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
axis lines = center,
axis line style = thick,
xlabel=$x$, ylabel=$y$,
ylabel style={left},
ymin=-3,
ymax=3.5,
xmin=-4,
xmax=8,
unit vector ratio=1 1,
width=15cm,
xtick=\empty, 
ytick=\empty
]
\addplot [name path=C, draw=none,samples=2] {0};
\addplot [name path=D, domain=1:3,samples=2] {1};
\addplot [name path=A,blue, very thick,samples=100, domain=-2:2] ({x^2+2},{x});
\addplot [name path=B,cyan, very thick,samples=100, domain=-2:2] ({x},{x^3});

\path [name path=BC,%draw=red,thick,->,
    intersection segments={of=B and C,
    sequence={A2[reverse] -- B2}, },];

\path [name path=BCA,%draw=orange,thick,->,
    intersection segments={of=BC and A,
    sequence={A0 -- B1}, },];

\path [fill=gray,opacity=0.2,%draw=purple,ultra thick,->,
    intersection segments={of=BCA and D,
    sequence={A1 -- B1[reverse]}, },];
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

或者用fill between

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
axis lines = center,
axis line style = thick,
xlabel=$x$, ylabel=$y$,
ylabel style={left},
ymin=-3,
ymax=3.5,
xmin=-4,
xmax=8,
unit vector ratio=1 1,
width=15cm,
xtick=\empty, 
ytick=\empty
]
% \fill[gray,opacity=0.2] plot[domain=0:1] (\x,\x*\x*\x) -- plot[domain=1:0] 
% ({\x*\x+2},{\x});
\addplot [name path=C, draw=none,samples=2] {0};
\addplot [name path=A,blue, very thick,samples=100, domain=-2:2] ({x^2+2},{x});
\addplot [name path=B,cyan, very thick,samples=100, domain=-2:2] ({x},{x^3});

\pgfonlayer{pre main}
\clip plot[domain=0:1] (\x,\x*\x*\x) -- plot[domain=1:0] 
({\x*\x+2},{\x}) --cycle;
\addplot [name path=D,samples=2] {1};
\addplot[gray,opacity=0.2] fill between [of=C and D];
\endpgfonlayer
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

最后用三个软夹子夹住。

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
axis lines = center,
axis line style = thick,
xlabel=$x$, ylabel=$y$,
ylabel style={left},
ymin=-3,
ymax=3.5,
xmin=-4,
xmax=8,
unit vector ratio=1 1,
width=15cm,
xtick=\empty, 
ytick=\empty,
clip=false
]
\addplot [name path=C, draw=none] {0};
\addplot [name path=D, domain=1:3] {1};
\addplot [name path=D', domain=3:1,draw=none] {1};
\addplot [blue, very thick,samples=101, domain=-2:2] ({x^2+2},{x});
\addplot [name path=A,draw=none,samples=51,domain=0:2] ({x^2+2},{x});
\addplot [name path=B,cyan, very thick,samples=101, domain=-1.5:1.5] ({x},{x^3});

\addplot[gray,opacity=0.2] fill between [of=C and B, soft clip={domain=0:1}];
\addplot[gray,opacity=0.2] fill between [of=C and D, soft clip={domain=1:2}];
\addplot[gray,opacity=0.2] fill between [of=A and D, soft clip={domain=2:3},reverse=true];

\end{axis}
\end{tikzpicture}

\end{document}

相关内容