0

I have two Guassian distribution curves, as shown here and I am attempting to color the areas under the curves as well as the intersection between them, like here:

colored curves

I assumed fillbetween would be the appropriate tool here, but that produced some strange results. Below is the code

\begin{tikzpicture}
    \begin{axis}[
    axis lines=left,
    domain=0:10, 
    samples=100,
    ymin=0,
    xtick=\empty,
    ytick=\empty,
    enlargelimits=false, 
    clip=false,
    ]

    \addplot [thick, orange, name path=cat] {gauss(x, 3, 1)} node[pos=0.3, above]{cat};
    \addplot [thick, purple!70!black, name path=puma] {gauss(x, 5, 0.5)} node[pos=0.5, above right] {puma};

    \path[name path=axis] (axis cs:0,0) -- (axis cs:10,0);

    \addplot[
        thick,
        color=orange,
        fill=yellow,
        fill opacity=0.25
    ]
    fill between[
        of=cat and axis
    ];

    \addplot[
        thick,
        color=purple!70!black,
        fill=purple!70!black,
        fill opacity=0.25
    ]
    fill between[
        of=puma and axis
    ];

    \addplot[
        thick,
        color=white,
        fill=blue,
        fill opacity=0.25
    ]
    fill between[
        of=cat and puma
    ];
    \end{axis}
\end{tikzpicture}

which produces this:

wrong curves

Every other answer I've found either miraculously makes fillbetween work or avoids it entirely (usually in ways that seem more complicated than is necessary for a simple task such as this).

Can anyone explain to me what is missing to make fillbetween achieve the desired results or recommend a clean alternative? Thanks in advance.

2
  • 1
    What exactly is your problem? What do you think should be different in your current output? It is hard to help you, if you neither exactly describe what the problem should be nor provide a minimal working example (MWE). Oct 23, 2017 at 13:59
  • Did my answer answer your question or do you need further assistance? In the first case please consider marking it as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). Oct 25, 2017 at 14:00

1 Answer 1

2

As already mentioned in the comment below the question next time you should either provide a full minimal working example (MWE) or state exactly what you expect from the code and where the error might be.

Here I provide the code to get the I output that I think you are searching for. For more details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{
        /pgf/declare function={
            % normal distribution where \mean = mean and \stddev = sd}
            gauss(\mean,\stddev) = 1/sqrt(2*pi*\stddev^2) * exp(-((\x-\mean)^2)/(2*\stddev^2));
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis lines=left,
        domain=0:10,
        samples=101,
        ymin=0,
        xtick=\empty,
        ytick=\empty,
        enlargelimits=false,
        clip=false,
    ]

        \addplot [
            thick,
            orange,
            name path=cat,
        ] {gauss(3, 1)}
            node[pos=0.3, above] {cat}
        ;
        \addplot [
            thick,
            purple!70!black,
            name path=puma,
        ] {gauss(5, 0.5)}
            node [pos=0.5, above right] {puma}
        ;

        \path[name path=axis] (axis cs:0,0) -- (axis cs:10,0);

        % because `cat' and `puma' only intersect once and both have a minimum
        % value of 0, both areas can be filled with one call without filling the
        % common area
        \addplot[
            thick,
        ] fill between [
            of=cat and puma,
            split,
            every segment no 0/.style={
                fill=yellow,
                fill opacity=0.25,
            },
            every segment no 1/.style={
                fill=purple!70!black,
                fill opacity=0.25
            },
        ];

        % compute + label the lower area (but do not draw it):
        \path [
            name path=lower,
            intersection segments={
                of=cat and puma,
                sequence=R1 -- L2,
            }
        ];

        % fill the lower area
        \addplot [
            fill=blue,
            fill opacity=0.25,
        ] fill between [
            of=axis and lower,
        ];

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

image showing the result of above code

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.