6

I'm trying to place a label above a curve with tikz and pgfplots. My solution below produces the expected output, but only when I ignore the error message during compiling:

Package PGF Error: I cannot decorate an empty path. [  \end{axis}]

What does it mean and how do I fix it? There clearly is a visible path, and it does get decorated when I skip over the error. Example below:

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9} 

\begin{document}

\pgfmathdeclarefunction{poiss}{1}{%
  \pgfmathparse{(#1^x)*exp(-#1)/(x!)}%
}

\begin{tikzpicture}[node distance=10mm]
    \begin{axis}[
      compat=newest,
      ymax=0.16,
      xlabel=$k$,
      ylabel=$P(k)$,
      domain=0:25,samples at = {0,...,25},
      label/.style 2 args={%
        postaction={ decorate,
           decoration={ markings, mark=at position #1 with \node#2;}
          }}%
      ]
      \addplot[red,mark=o,%
               label={0.45}%
                     {[above]{$\lambda=10$}}%
        ] {poiss(10)};
  \end{axis}
\end{tikzpicture}

\end{document}
jdm
  • 1,035

2 Answers2

4

The problem was that mark seems to be incompatible with decorate. It works if you draw the plot twice, once with markings, and once with decorations.

(Bonus: This also shows how to add [] arguments to the node that is placed as a decoration; I had difficulties with that, since it doesn't parse [above,yshift=-4pt]. You can define the style externally, as below. Alternatively you can say \node [above][blue], but that only seems to work if there are no , and = in the node's arguments. )

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9} 

\begin{document}

\pgfmathdeclarefunction{poiss}{1}{%
  \pgfmathparse{(#1^x)*exp(-#1)/(x!)}%
}

\tikzstyle{higherabove}+=[above,yshift=4pt]

\begin{tikzpicture}[node distance=10mm]
    \begin{axis}[
      compat=newest,
      ymax=0.16,
      xlabel=$k$,
      ylabel=$P(k)$,
      domain=0:25,samples at = {0,...,25},
      mylabel/.style n args={2}{%
        postaction={decorate},%
        decoration={markings,%
                    mark=at position #1 with \node [higherabove] {#2}; }%
      }%
      ]

      \addplot[red,mark=o] {poiss(10)};
      \addplot[red,mylabel={0.45}{hello} ] {poiss(10)};

  \end{axis}
\end{tikzpicture}

\end{document}
jdm
  • 1,035
4

PGFPlots provides a native way to add labels to plots without having to resort to the decorations library: you simply add node [pos=..., <options>] {text}; to the \addplot command:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\pgfmathdeclarefunction{poiss}{1}{%
  \pgfmathparse{(#1^x)*exp(-#1)/(x!)}%
}


\begin{tikzpicture}
    \begin{axis}[
      compat=newest,
      ymax=0.16,
      xlabel=$k$,
      ylabel=$P(k)$,
      domain=0:25,samples at = {0,...,25}
      ]

      \addplot[red, mark=o] {poiss(10)} node [pos=0.4, above=4pt] {hello};

  \end{axis}
\end{tikzpicture}

\end{document}

Jake
  • 232,450