1

I have plotted some values on a pgfplot and I wish to color in red the part under the line. Some suggestion about a package or what I need to look into would be great. In this case googling it did not help me too much

My graph: enter image description here

my code:

\documentclass{article}

\begin{document}
\usepackage{pgfplots}
\usetikzlibrary{datavisualization}
\usepackage{tikz}
\begin{tikzpicture}
\datavisualization [school book axes, visualize as smooth line]
    data {
       x, y
    -1.9,0.001
-1.8,0.001
-1.7,0.002
-1.6,0.001
-1.5,0.01
-1.4,0.004
-1.3,0.005
-1.2,0.008
-1.1,0.01
-1,0.014
-0.9,0.029
-0.8,0.037
-0.7,0.056
-0.6,0.119
-0.5,0.187
-0.4,0.391
-0.3,0.667
-0.2,1.062
-0.1,1.648
0,2.418
0.1,2.454
0.2,1.829
0.3,1.243
0.4,0.809
0.5,0.537
0.6,0.319
0.7,0.178
0.8,0.101
0.9,0.06
1,0.035
1.1,0.019
1.2,0.018
1.3,0.014
1.4,0.019
1.5,0.004
1.6,0.004
1.7,0.002
1.8,0.003
1.9,0.001
2,0.003
2.1,0.001
2.2,0.002
2.3,0.001
2.4,0.001
};
\end{tikzpicture}


\end{document}

I want it to look something like this:

enter image description here

  • @StefanPinnow, I don't understand how to use the fillbetween from the other question which you referenced. I don't have 2 plots. I have one line.. – Adrian May 13 at 12:06
3

This can be done using e.g. PGFPlots fillbetween library. Please also have a look at the corresponding section in the PGFPlots manual or at other questions here on TeX.SX with the tag .

% used PGFPlots v1.16
\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));
            % define xmin and xmax only once here and reuse them later
            xmin=-2;
            xmax=2;
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis x line=bottom,
        axis y line=middle,
        axis on top,
        xmin=xmin,
        xmax=xmax,
        domain=xmin:xmax,
        samples=100,
    ]
        \path[name path=axis] (axis cs:xmin,0) -- (axis cs:xmax,0);

        \addplot [
            thick,
            name path=gauss,
        ] {gauss(0.1, 0.25)};
        % instead of using a function here you could also give table values or read a (table) file
       % \addplot table {<table> or <file name>};

        \addplot [
            fill=black!10,
        ] fill between [
            of=axis and gauss,
        ];
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • Oh wow, I did not expect such a detailed answer. Thank you so much for your help and patience! – Adrian May 13 at 12:51

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.