[Tex/LaTex] Pgfplots axis, option to remove (or ignore) legends

legendpgfplots

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
    ]
    \addlegendentry{graph 1} %ignored
    \addplot+[mark=none,fill=yellow,draw=none,forget plot] {0.1*x^2};
    \addlegendentry{graph 2} %ignored
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addlegendentry{graph 3} %ignored
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
  \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.

Best Answer

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 
    ]
    \addlegendentry{graph 1} %ignored
    \addplot+[mark=none,fill=yellow,draw=none,forget plot] {0.1*x^2};
    \addlegendentry{graph 2} %ignored
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addlegendentry{graph 3} %ignored
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
  \end{axis}
\end{tikzpicture} 
\end{document}

enter image description here

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
    ]
    \addlegendentry{graph 1} %ignored
    \addplot+[mark=none,fill=yellow,draw=none,forget plot] {0.1*x^2};
    \addlegendentry{graph 2} %ignored
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addlegendentry{graph 3} %ignored
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
  \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][]{}}}}

instead of

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