4

I am creating a plot in pgfplots that consists of a function and a set of coordinates, as seen in this MWE

\documentclass[tikz]{standalone}
\usepackage{tikz, pgfplots}

\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro\pointa{exp(-1)}
    \pgfmathsetmacro\pointb{exp(-1.25)}
    \pgfmathsetmacro\pointc{exp(-1.5)}
    \pgfmathsetmacro\pointd{exp(-1.75)}
    \pgfmathsetmacro\pointe{exp(-2)}
    \pgfmathsetmacro\pointf{exp(-2.25)}
    \pgfmathsetmacro\pointg{exp(-2.5)}
    \pgfmathsetmacro\pointh{exp(-2.75)}
    \pgfmathsetmacro\pointi{exp(-3)}
    \pgfmathsetmacro\pointj{exp(-3.25)}
    \pgfmathsetmacro\pointk{exp(-3.5)}
    \pgfmathsetmacro\pointl{exp(-3.75)}
    \pgfmathsetmacro\pointm{exp(-4)}
    \pgfmathsetmacro\pointn{exp(-4.25)}
    \pgfmathsetmacro\pointo{exp(-4.5)}
    \pgfmathsetmacro\pointp{exp(-4.75)}
    \pgfmathsetmacro\pointq{exp(-5)}
    \begin{axis}[xlabel={Time difference (timing units)},
                 ylabel={counts (arbirtary units)},
                     xmin=0, xmax=5.5,
                     ymin=0, ymax=1.1,
                     domain={0:5},
                 samples=100,
                 grid=major
                 %axis lines=left
                 ]
        \addplot[mark=none, thick, color=red, dashed] {exp(-x)};\addlegendentry{Theoretical Distribution}
        \addplot[color=black, fill=black, only marks, mark=*] coordinates{
        (0,0)
        (0.25,0.025)
        (0.5, 0.125)
        (0.75,0.25)
        (1,\pointa)
        (1.25,\pointb)
        (1.5,\pointc)
        (1.75,\pointd)
        (2,\pointe)
        (2.25,\pointf)
        (2.5,\pointg)
        (2.75,\pointh)
        (3,\pointi)
        (3.25,\pointj)
        (3.5,\pointk)
        (3.75,\pointl)
        (4,\pointm)
        (4.25,\pointn)
        (4.5,\pointo)
        (4.75,\pointp)
        (5,\pointq)};\addlegendentry{Dummy experimental points}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

What I would like to do is fill the space between the black points and the red dashed line as seen in the picture with gray shading. Then I would like to add a legend entry for this shaded area.

Any idea on how to do that?

9

The fillbetween library is for things like this. Note the addition of name path for both plots, and the removal of fill=black from the second one. Because the filling didn't work with an only marks plot, I removed only marks and added draw opacity=0.

Note that instead of defining all those \point macros, you can use e.g. (1.5,{exp(-1.5)}) in the coordinate list. The extra set of braces is needed to hide the parentheses in exp(..) from the parser.

output of code

\documentclass[tikz]{standalone}
\usepackage{pgfplots} % loads tikz
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \pgfmathsetmacro\pointa{exp(-1)}
    \pgfmathsetmacro\pointb{exp(-1.25)}
    \pgfmathsetmacro\pointc{exp(-1.5)}
    \pgfmathsetmacro\pointd{exp(-1.75)}
    \pgfmathsetmacro\pointe{exp(-2)}
    \pgfmathsetmacro\pointf{exp(-2.25)}
    \pgfmathsetmacro\pointg{exp(-2.5)}
    \pgfmathsetmacro\pointh{exp(-2.75)}
    \pgfmathsetmacro\pointi{exp(-3)}
    \pgfmathsetmacro\pointj{exp(-3.25)}
    \pgfmathsetmacro\pointk{exp(-3.5)}
    \pgfmathsetmacro\pointl{exp(-3.75)}
    \pgfmathsetmacro\pointm{exp(-4)}
    \pgfmathsetmacro\pointn{exp(-4.25)}
    \pgfmathsetmacro\pointo{exp(-4.5)}
    \pgfmathsetmacro\pointp{exp(-4.75)}
    \pgfmathsetmacro\pointq{exp(-5)}
    \begin{axis}[xlabel={Time difference (timing units)},
                 ylabel={counts (arbirtary units)},
                     xmin=0, xmax=5.5,
                     ymin=0, ymax=1.1,
                     domain={0:5},
                 samples=100,
                 grid=major
                 %axis lines=left
                 ]
        \addplot[mark=none, thick, color=red, dashed,name path=A] {exp(-x)};
        \addlegendentry{Theoretical Distribution}
        \addplot[color=black, draw opacity=0, mark=*,name path=B] coordinates{
        (0,0)
        (0.25,0.025)
        (0.5, 0.125)
        (0.75,0.25)
        (1,\pointa)
        (1.25,\pointb)
        (1.5,\pointc)
        (1.75,\pointd)
        (2,\pointe)
        (2.25,\pointf)
        (2.5,\pointg)
        (2.75,\pointh)
        (3,\pointi)
        (3.25,\pointj)
        (3.5,\pointk)
        (3.75,\pointl)
        (4,\pointm)
        (4.25,\pointn)
        (4.5,\pointo)
        (4.75,\pointp)
        (5,\pointq)};
        \addlegendentry{Dummy experimental points}

        \addplot [fill=black!20] fill between[of=A and B];
        \addlegendentry{Shaded area}
    \end{axis}
\end{tikzpicture}
\end{document}

Slight variation: split the second plot in two, with explicit coordinates up to x=1, and a plot of exp(-x) after that:

\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots} % loads tikz
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[xlabel={Time difference (timing units)},
                 ylabel={counts (arbirtary units)},
                     xmin=0, xmax=5.5,
                     ymin=0, ymax=1.1,
                     domain={0:5},
                 samples=100,
                 grid=major,
                 dotplot/.style={
                   black, draw opacity=0, mark=*}
                 ]
        \addplot[mark=none, thick, color=red, dashed,name path=A] {exp(-x)};
        \addlegendentry{Theoretical Distribution}
        \addplot[dotplot,name path=B] coordinates{
        (0,0)
        (0.25,0.025)
        (0.5, 0.125)
        (0.75,0.25)
        (1,{exp(-1)})};
        \addplot [forget plot,dotplot,samples at={1,1.25,...,5}] {exp(-x)};
        \addlegendentry{Dummy experimental points}

        \addplot [fill=black!20] fill between[of=A and B,soft clip={domain=0:1}];
        \addlegendentry{Shaded area}
    \end{axis}
\end{tikzpicture}
\end{document}
  • Thank you very much for your answer! Can you also add a legend entry? Thanks in advance! – Thanos Nov 23 '17 at 15:21
  • @Thanos Sorry, forgot that. But you already know how to do that: add \addlegendentry{Something}, – Torbjørn T. Nov 23 '17 at 15:22

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.