2

I managed to color the domain limited by the two curves without using the macro fillbetween because the instruction \addplot[gray] fill between[of=F and G, soft clip={domain=0:4}]; does not give the right drawing. How to do ?

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

enter image description here

2 Answers 2

1

Here is a very simple way: instead of two functions patched together manually, patch them in a parametric plot, then there is no problem. This method has the further advantage that the line joins look good. (In your example, the line join is covered by the thick axis, but in general this is an issue.)

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

enter image description here

0
2

In the spirit of full disclosure I must say that when you change soft clip from {domain=0.01:0.5} to {domain=0.0:0.5} the solution stops working, which I actually do not understand the reason for

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

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .