TransWikia.com

Filling with color between functions

TeX - LaTeX Asked on August 26, 2021

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

2 Answers

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}
    
newcommandxa{.092}
newcommandxb{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.501616*x + 59.02336*x^2 - 252.9963*x^3 + 359.5232*x^4};
addplot[name path=sh,color=blue, domain=0:0.5, samples=200,thick] {-2.377416 + 44.36774*x - 182.5977*x^2 + 333.9887*x^3 - 225.183*x^4};
addplot[name path=is,color=green,domain=0:0.5, samples=200, dotted,thick] {(x^2)/((1-x)*(0.01863372893)+x^2*0.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

Correct answer by pluton on August 26, 2021

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

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

real f(real x){return 0.3809462-3.501616*x+59.02336*x^2-252.9963*x^3 + 359.5232*x^4;}
real g(real x){return -2.377416+44.36774*x-182.5977*x^2+333.9887*x^3 - 225.183*x^4;}
real h(real x){return (x^2)/((1-x)*(0.01863372893)+x^2*0.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

Answered by user213378 on August 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP