# How can I create a legend of an envelope plot?

I making envelope plots which shows the minimum median max of my values. but when i try to create a legend it doesnt pick the median which has the right color as seen in the image below:

 \documentclass[preview,convert={density=300,size=1080x800,outext=.png}]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{standalone}
\usepgfplotslibrary{fillbetween}
\begin{document}
\resizebox{0.5\textwidth}{!}{\begin{tikzpicture}
\begin{semilogyaxis}[
ticklabel style = {font=\small},
ytick={1,1.e-2,1.e-4,1.e-6,1.e-8,1.e-10,1.e-12},
ymax=1,
ymin=1.e-15,
xmin=1,
xmax=30,
xlabel=\large{CPU (s)},
ylabel=\large{RMSE},
]

% vani
\addplot+[name path=A,red!80!black,opacity=0.3,mark=none] table[x expr=\coordindex,y=Off_min_Vani, col sep=space]{Vani_fixed_10.dat};
\addplot[red,line width=2pt] table[x expr=\coordindex,y=Off_med_Vani, col sep=space]{Vani_fixed_10.dat};
\legend{{Method 1}}

\end{semilogyaxis}
\end{tikzpicture}
}

\end{document}


The \legend-command expects the legend entries of every plot in the exact order they have been created by \addplot. For your example \legend{A, B, fill, Method 1} would describe every \addplot-instance correctly. If you want an instance of \addplot to be ignored in the legend, you have to add forget plot as an option.

 \documentclass[preview,convert={density=300,size=1080x800,outext=.png}]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{standalone}
\usepgfplotslibrary{fillbetween}
\begin{document}
\resizebox{0.5\textwidth}{!}{\begin{tikzpicture}
\begin{semilogyaxis}[
ticklabel style = {font=\small},
ytick={1,1.e-2,1.e-4,1.e-6,1.e-8,1.e-10,1.e-12},
ymax=1,
ymin=1.e-15,
xmin=1,
xmax=30,
xlabel=\large{CPU (s)},
ylabel=\large{RMSE},
]

% vani
\addplot+[name path=A,red!80!black,opacity=0.3,mark=none, forget plot] table[x expr=\coordindex,y=Off_min_Vani, col sep=space]{Vani_fixed_10.dat};
\addplot+[name path=B,red!80!black,opacity=0.3,mark=none, forget plot]table[x expr=\coordindex,y=Off_max_Vani, col sep=space]{Vani_fixed_10.dat};
\addplot[red!80!black,opacity=0.3, forget plot] fill between[of=A and B];
\addplot[red,line width=2pt] table[x expr=\coordindex,y=Off_med_Vani, col sep=space]{Vani_fixed_10.dat};
\legend{{Method 1}}

\end{semilogyaxis}
\end{tikzpicture}
}

\end{document}


Disclaimer: I was not able to check if the above code compiles correctly, as I do not want to download files from external websites. Please provide a MWE the next time!

Something off-topic: Do not use \resizebox to scale figures/pictures/plots as this will also scale the font. Use the appropriate width option provided by pgfplots instead!

• Awsome it works perfectly. Any idea how to pop-out the legend and place it outside the plot to the right? – Farouk Yahaya Jun 22 at 9:30
• nevermind i did it with: legend pos=outer north east]. Thanks a lot for your help! – Farouk Yahaya Jun 22 at 9:35