4

I need some help in filling with colors between some functions, here is my code:

 \documentclass[12pt]{report}
    \usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[ xlabel = {$p$}, ylabel = {$\lambda$}, 
                    xmin=0,   xmax=0.5,
                    ymin=0,   ymax=2]   
        \addplot [name path=ih, domain=0:0.5, samples=100] {0.3809462 - 3.501616*x + 59.02336*x^2 - 252.9963*x^3 + 359.5232*x^4};
        \addplot [name path=sh, domain=0:0.5, samples=100] {-2.377416 + 44.36774*x - 182.5977*x^2 + 333.9887*x^3 - 225.183*x^4};
        \addplot [name path=is, domain=0:0.5, samples=100, dotted] {(x^2)/((1-x)*(0.01863372893)+x^2*0.5)};
        \end{axis}
    \end{tikzpicture}
\end{document}

I would like it to looks like this:

description

Do you have any suggestion on how to do so? Thanks

fennel
  • 97

2 Answers2

8

You are not far. You need some clipping with respect to the x values via the fill between[soft clip={domain=\xa:\xb}]; command for the green area. For the red and blue areas, you also have to use the fillbetween library with the top and bottom horizontal axes, hence \path[name path=baxis] (axis cs:0,0) -- (axis cs:1,0); and \path[name path=taxis] (axis cs:0,2) -- (axis cs:1,2); to create the bottom and top axes. I have also added colors to the lines for guidance.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}

\newcommand\xa{.092} \newcommand\xb{0.42}

\begin{document} \begin{tikzpicture} \begin{axis}[xlabel={$p$},ylabel={$\lambda$},xmin=0,xmax=0.5,ymin=0,ymax=2]
\addplot[name path=ih,color=red,domain=0:0.5, samples=200,thick] {0.3809462 - 3.501616x + 59.02336x^2 - 252.9963x^3 + 359.5232x^4}; \addplot[name path=sh,color=blue, domain=0:0.5, samples=200,thick] {-2.377416 + 44.36774x - 182.5977x^2 + 333.9887x^3 - 225.183x^4}; \addplot[name path=is,color=green,domain=0:0.5, samples=200, dotted,thick] {(x^2)/((1-x)(0.01863372893)+x^20.5)};

% red shading \path[name path=baxis] (axis cs:0,0) -- (axis cs:1,0); \addplot [red!50]fill between[of=is and baxis,soft clip={domain=0:.5}];

% blue shading \path[name path=taxis] (axis cs:0,2) -- (axis cs:1,2); \addplot [blue!50]fill between[of=is and taxis,soft clip={domain=0:.5}];

% green shading \addplot[green!50] fill between[of=ih and sh,soft clip={domain=\xa:\xb}];

\end{axis} \end{tikzpicture} \end{document}

enter image description here

pluton
  • 16,421
5

With Asymptote, http://asymptote.ualberta.ca/

import graph;
size(6cm,5cm,false);

real f(real x){return 0.3809462-3.501616x+59.02336x^2-252.9963x^3 + 359.5232x^4;} real g(real x){return -2.377416+44.36774x-182.5977x^2+333.9887x^3 - 225.183x^4;} real h(real x){return (x^2)/((1-x)(0.01863372893)+x^20.5);}

fill(graph(h,0,0.5)--(0.5,0)--cycle,red); fill(graph(h,0,0.5)--(0.5,2)--(0,2)--cycle,blue); fill(buildcycle(graph(f,0,0.5),graph(g,0,0.5)),green);

draw(graph(f,0,0.5)); draw(graph(g,0,0.5)); draw(graph(h,0,0.5),dotted);

clip(box((0,0),(0.5,2))); draw(box((0,0),(0.5,2)));

for (real i: new real[]{0,0.5,1,1.5,2}){ label(scale(0.6)Label("$"+(string) i+"$"),(0,i),dir(180)); } for (real i: new real[]{0,0.1,0.2,0.3,0.4,0.5}){ label(scale(0.6)Label("$"+(string) i+"$"),(i,0),dir(-90)); }

label(rotate(90)*"$\lambda$",(-0.08,1)); label("$p$",(0.25,-0.15),dir(-90)); shipout(bbox(1mm,invisible));

enter image description here

  • Considering my sincere compliments for you skills...there is a question for you: https://tex.stackexchange.com/questions/558933/part-of-sphere-with-asymptote-3d :-) – Sebastiano Aug 17 '20 at 08:03
  • @Sebastiano But i don't have much knowledge of 3D... I am still learning Asymptote. :-) –  Aug 17 '20 at 08:11
  • 1
    For my humble opinion you are very good. – Sebastiano Aug 17 '20 at 08:13