# 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

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}
\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.

Option 3

If you are freshly writing the code, then simply omit the command \addlegendentry{graph 3} etc.

-
I'd go the whole nine yards and use \pgfplotsset{ignore legend/.style={every axis legend/.code={\let\addlegendentry\relax}}} in the preamble so that the OP can use their “hypothetical” option ignore legend. –  Qrrbrbirlbel Oct 20 '12 at 23:53
Note that this is a very questionable hack and might be breaking stuff :). I'd rather wait for Christian to approve since he knows it inside out. –  percusse Oct 21 '12 at 0:34
@HarishKumar 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. –  Christian Feuersänger Oct 25 '12 at 8:55
As pointed out by @ChristianFeuersänger, the \relax can lead to unintentional stuff. I had probelms, just by having math symbols in my legend (which I consider pretty standard and nothing exotic), e.g., $\gamma=0.5$. The \renewcommand option is definitely the better one. I think you should remove the \relax solution entirely. –  Clausen Jun 14 '14 at 0:09

What about typing \legend{} before \end{axis}?

-
Welcome to TeX.SX! Is this a follow-up question for clearification? –  Christian Hupfer Feb 13 at 14:46
That works well. You should expand this to be a complete answer, including a full minimal document demonstrating that it works. –  Andrew Swann Feb 13 at 14:47