2

I'm trying to fill the area between two curves. Ideally, what I want to do is already answered here, but I get the following warning (twice):

Package pgf Warning: fill between skipped: the first input path is empty. on input line 48.

And this is what I have tried so far

\documentclass{article}
\usepackage{tikz,graphicx}
\usepackage{pgfplots, pgfplotstable}
    \usepgfplotslibrary{statistics}
    \usepgfplotslibrary{fillbetween}
    \usetikzlibrary{patterns,intersections}
    \pgfplotsset{compat=newest}
    \usetikzlibrary{babel}
\begin{document}
            \begin{figure}%[ht]
                \centering
                \begin{tikzpicture}
                    \begin{axis}[
                        xmin=-2,xmax=7,
                        ymin=0,ymax=1.5,
                        axis x line=middle,
                        axis y line=middle,
                        axis line style=-,
                        xlabel={$x$},
                        ylabel={$y$},
                        ]
                        % parte negra de f1
                        \addplot[no marks,black,-] expression[domain=-2:3,samples=100]{exp(-0.5*(x-2)^2)} 
                                    node[pos=-0.5,anchor=north east]{};
                        
                        % parte negra de f2
                        \addplot[no marks,black,-] expression[domain=3:7,samples=100]{exp(-0.5*(x-4)^2)} 
                                    node[pos=-0.5,anchor=north east]{};

                        % parte azul de f1
                        \addplot+[no marks,blue,thick,-] [name path=f1,domain=3:7,samples=1000]{exp(-0.5*(x-2)^2)} 
                                    node[pos=0,anchor=north east]{};
                        
                        % parte azul de f2
                        \addplot[no marks,blue,thick,-] expression[name path=f2,domain=-2:3,samples=1000]{exp(-0.5*(x-4)^2)} 
                                    node[pos=0,anchor=north east]{};

                        \draw (2,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk1){$f_1(x)$};
                        
                        \draw (4,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk2){$f_2(x)$};

                        % eje x
                        \addplot[name path=xAxis,const plot,thick,no marks,draw=none] coordinates {(0,0) (1,0)};

                        \addplot[blue!20!white] fill between[of=f1 and xAxis,soft clip={domain=3:7}];
                        \addplot[blue!20!white] fill between[of=f2 and xAxis,soft clip={domain=-2:3}];

                    \end{axis}
                \end{tikzpicture}
                \caption{Función logística o sigmoidal}
                \label{fig:Two plots}
            \end{figure}
\end{document}

which should render to

enter image description here

with the area under the blue segments filled. As a bonus, I'd like the x label not to collide with the curve. Any ideas where to add `name path global=axis?

2 Answers 2

3

I think I can present a much simpler way to achieve what you want. For more details please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{
        % use this `compat' level or higher so you don't need to specify
        % `axis cs:' any more at each TikZ coordinate
        compat=1.11,
        % declare the functions here so it is easy to calculate the positions
        % of the labels as well
        /pgf/declare function={
            f1(\x) = exp(-0.5*(\x-2)^2);
            f2(\x) = exp(-0.5*(\x-4)^2);
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xmin=-2,xmax=7,
        ymin=0,ymax=1.5,
        % I changed that from `middle' to `bottom', so that also the 0
        % is shown as label ...
        axis x line=bottom,
        axis y line=middle,
        axis line style=-,
        xlabel={$x$},
        ylabel={$y$},
        xlabel style={
            % ... but because of that you need to re-position `xlabel' and
            % the corresponding node anchor if it should be on the end of
            % the x-axis
            at={(xticklabel cs:1)},
            anchor=south east,
        },
        % use this key to draw the axis lines over the other stuff
        axis on top,
        % moved all common `\addplot' options here
        no markers,
        domain=-2:7,
        samples=101,
    ]
        % you only need to plot the functions once
        % (and of course you can use the above defined functions here)
        \addplot [name path=f1] {f1(x)};
        \addplot [name path=f2] {f2(x)};

        % and of course you can also use the declared functions to calculate
        % the proper position of the labels
        % (as stated above, using `compat=1.11' or higher you don't need
        %  to state, e.g. `(axis cs:2,{f1(2)})' any more)
        \node [anchor=south] at (2,{f1(2)}) {$f_1(x)$};
        \node [anchor=south] at (4,{f2(4)}) {$f_2(x)$};

        % even simpler than using an `\addplot' command here is just
        % specifying a path
        % You can use the following line when you explicitly have set the
        % `xmin' and `xmax' values. ...
        % (Of course you can also call this values in `\addplot coordinates')
        \path [name path=xAxis] (\pgfkeysvalueof{/pgfplots/xmin},0)
            -- (\pgfkeysvalueof{/pgfplots/xmax},0);
%        % ... In case `xmin' and `xmax' are not specified, you can use
%        % the following to achieve the same result
%            \path (rel axis cs:0,0) |- (0,0) node [coordinate] (xmin) {};
%            \path (rel axis cs:1,0) |- (0,0) node [coordinate] (xmax) {};
%        \path [name path=xAxis] (xmin) -- (xmax);

        % now you can immediatly draw the thick black line using the
        % `sequence' feature of the `fillbetween' library to which you also
        % give a name to later use it to fill the area below it
        \draw [
            name path=lower,
            draw=blue,
            thick,
            % this is an option from the `fillbetween' library of PGFPlots
            % to create a path along the curves from intersection point to
            % intersection point
            intersection segments={
                of=f1 and f2,
                sequence=R1 -- L2,
            },
        ];

        % doing the stuff above now it is very easy to fill the area under
        % the blue line
        \addplot [blue!20!white] fill between [of=lower and xAxis];

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

image showing the result of above code

5
  • Your code looks great, specially the part of x label not colliding with the graph and grouping common options. I have some silly questions about it: when declaring the functions, why do you use \x and compat=1.12In the X label style, what does cs mean? where did you define R2 and L1 in the last \draw call? (we can take this conversation to chat)
    – mrbolichi
    Commented Mar 30, 2017 at 9:44
  • Thank you very much. To make it short for your questions: Regarding declare function, xticklabel cs and intersection segments you will find the answers in the manuals of TikZ and PGFPlots. Regarding the compat level, please have a look at the comment in the code. To make the code as is it is required to use this compat level or higher. What the compat level is good for/does do, please also have a look at the PGFPlots manual section 2.2.1. Commented Mar 30, 2017 at 13:54
  • @c4tich, did that answer your questions and solve your problem. Then you can think of accepting one of the answers (by clicking on the checkmark ✓), so your question doesn't show up in the "still open" list. Thank you. Commented Apr 1, 2017 at 11:46
  • Sorry, it was a very long weekend.
    – mrbolichi
    Commented Apr 3, 2017 at 13:50
  • Not a problem. Thank you for following the advice. Commented Apr 3, 2017 at 14:14
1

I think you want only to clip the second path.

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics,fillbetween}
\usetikzlibrary{patterns,intersections}
\pgfplotsset{compat=newest}
\usetikzlibrary{babel}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xmin=-2,xmax=7,
    ymin=0,ymax=1.5,
    axis x line=middle,
    axis y line=middle,
    axis line style=-,
    xlabel={$x$},
    ylabel={$y$},
    ]
    % parte negra de f1
    \addplot[no marks, black, -] expression [domain=-2:3, samples=100] {exp(-0.5*(x-2)^2)} node [pos=-0.5,anchor=north east]{};
%     % parte negra de f2
    \addplot[no marks, black, -] expression [domain=3:7, samples=100] {exp(-0.5*(x-4)^2)} node [pos=-0.5, anchor=north east]{};
    parte azul de f1
    \addplot+[no marks, blue, thick, -, name path=f1, domain=3:7, samples=1000] expression [] {exp(-0.5*(x-2)^2)} node [pos=0,anchor=north east]{};
    % parte azul de f2
    \addplot+[no marks, blue,thick, -, name path=f2, domain=-2:3, samples=1000] expression [] {exp(-0.5*(x-4)^2)} node [pos=0, anchor=north east]{};

    \draw (2,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk1){$f_1(x)$};
    \draw (4,1) ++(90:0.2cm) ++(90:0.25cm) node [fill=none](fk2){$f_2(x)$};
    % manual p. 429
    \path [name path=xAxis] (\pgfkeysvalueof{/pgfplots/xmin},0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);

    % manual p. 431
    \addplot [blue!20!white] fill between[of=f1 and xAxis, soft clip second={domain=3:7}];
    \addplot [blue!20!white] fill between[of=f2 and xAxis, soft clip second={domain=-2:3}];
  \end{axis}
\end{tikzpicture}
\end{document}

clipped to second paths

5
  • Amazing! Can you explain what is being done in the line where the x axis is defined?
    – mrbolichi
    Commented Mar 29, 2017 at 22:34
  • @c4tich I copied that line from the manual, but forgot to note it. However, that didn't actually make any difference :(. The second stuff is what made the difference.
    – cfr
    Commented Mar 29, 2017 at 22:35
  • @c4tich page 429. I've added the note in the code. And 431 for the second stuff.
    – cfr
    Commented Mar 29, 2017 at 22:37
  • I'm sorry, I couldn't find it. I read the manual available with texdoc tikz which is 1161 pages long and version 3.0.1.a
    – mrbolichi
    Commented Mar 30, 2017 at 9:35
  • @c4tich The pgfplots manual.
    – cfr
    Commented Mar 30, 2017 at 12:23

You must log in to answer this question.

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