1

I've seen enough simmilar topics, but after tweaking fill between option for so long I couldn't get the right fill between curves and y=1.

This is the best result I could achieve so far. enter image description here

Here is the code:

\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}
3

There is a general solution to this: combine the intersection segments of two paths to a new path, combine this one with the next path and then combine this with the last one. However, for that one always has to guess the segment number.

This example is so simple that one can just do

\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}

enter image description here

Here is the general method.

\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}

enter image description here

Or with 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}

enter image description here

Finally, with three soft clips.

\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}
  • Thanks! I see you are using \fill. I'm wondering whether it's possible to achieve such a resul using \addplot and fill between? – antshar Apr 30 '20 at 1:09
  • @antshar In principle, yes, but why? It would just give the same output and is more work. I added the method with intersection segments. – user194703 Apr 30 '20 at 1:15
  • I'd really love to see how it is done. Could you tell how to do that? – antshar Apr 30 '20 at 1:22
  • @antshar I added it. Please note that this does not decompose the shaded region in smaller pieces, which leads to artifacts on certain viewers. – user194703 Apr 30 '20 at 1:24
  • Oh, it still requires some additional stuff like \clip. I thought I could make do with only \addplot. Nevertheless, thank you so much for all these methods! – antshar Apr 30 '20 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.