I'm trying to use the result from a fill between
as a legend entry, but I can't get it to work:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}
\addplot[name path = upper, draw = none] {5 + rand};
\addplot[name path = lower, draw = none] {-5 + rand};
\addplot[black!10, area legend] fill between[of = upper and lower];
\addlegendentry{My interval}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
gives
with a line in the legend image instead of the rectangle I expected from using area legend
in the options to \addplot
.
I tried providing a legend image manually using
\addlegendimage{\draw[fill = black!10] (0cm, -0.1cm) rectangle (0.6cm, 0.1cm)}
but that did nothing. (The \draw
code is inspired by Section 4.9.5 in the pgfplots manual and is supposed to be the default appearance of a area legend
entry.)
Any ideas? Thanks in advance.
Edit:
Adding area legend
to the axis option gets me halfway there, but the fill is still not right.
Best Answer
The comment by John Kormylo put me on the right track. The key is to make the legend refer to the third plot only by issuing
forget plot
to the two previous ones. The following works: