# Pgfplots axis, option to remove (or ignore) legends

Is there an option in pgfplots at the axis level or a setting of legend style option, that would effectively ignore the subsequent legend entries in the plot?

\documentclass{article} \usepackage{pgfplots}
\begin{document} \begin{tikzpicture}
\begin{axis}[legend style = {at={(1.03,-0.03)}},
%ignore legend %hypothetical option
]
\end{axis}
\end{tikzpicture} \end{document}


I have a large plot (many curves) and I want to remove all the legends at the same time, without modifying the code too much.

-
 As a quick hack you can skip all the \addlegendentry commands by assigning it to \relax. So put every axis legend/.code={\let\addlegendentry\relax} to the hypothetical line and it will locally cancel all the legend entries. – percusse Oct 20 '12 at 21:06 @percusse, yes, I was looking for a hack like this. However when I add your option, the whole text for legends appears in text mode before a plot with no legends. – alfC Oct 20 '12 at 21:26 Strange I don't see anything. I'm using v1.6. – percusse Oct 20 '12 at 21:34 @percusse: Your suggestion seems to work just fine for me as well. I am not able to reproduce the problem that alfC experiences with your approach. – Peter Grill Oct 20 '12 at 21:37 @PeterGrill Maybe it's a better idea to gobble the argument too to avoid those cases (though I can't replicate it) but I'll wait in case Christian chips in. – percusse Oct 21 '12 at 4:12
show 2 more comments

Here is what can be done for your problem.

Option 1

As suggested by percusse every axis legend/.code={\let\addlegendentry\relax} can be used to suppress legends. Following code shows their use both locally and globally.

Code:

\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{every axis legend/.code={\let\addlegendentry\relax}} %%%if you don't want them globally, uncomment this line

\begin{document}
\begin{tikzpicture}
\begin{axis}[legend style = {at={(1.03,-0.03)}},
every axis legend/.code={\let\addlegendentry\relax}   %ignore legend locally
]
\end{axis}
\end{tikzpicture}
\end{document}


Option 2

As suggested by Qrrbrbirlbel, you can define a new style by

\pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}}


in the preamble and now you can use your hypothetical ignore legend as you wished.

Code:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend style = {at={(1.03,-0.03)}},
ignore legend %hypothetical option! NOT ANYMORE
]
\end{axis}
\end{tikzpicture}
\end{document}


As Christian Feuersänger writes in his comment,

It is save to overwrite this macro to no-operation. However, your solution does not really overwrite it to no-operation, because it results in \relax{graph 1} - and {graph 1} is still processed by TeX. In your case, the characters are silently ignored... but if you write complicated things in the argument, it might fail.

and his suggestion is to use

\pgfplotsset{ignore legend/.style={every axis legend/.code={\renewcommand\addlegendentry[2][]{}}}}


\pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}}

since \renewcommand takes care of both the mandatory and the optional argument of \addlegendentry.
If you are freshly writing the code, then simply omit the command \addlegendentry{graph 3} etc.