8

I am forced to make rather small plots that need to have a legend. This means that it is unavoidable that part of the plot gets covered by the legend.

To retain legibility of the plot, I would like to make the background of the legend partly transparent. My approach was to use legend style={fill opacity=0.8, draw opacity=1}, however, this makes the whole legend transparent, and not only the fill.

Here is an MWE:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=5cm,
            axis equal,
            legend style={fill=white, fill opacity=0.6, draw opacity=1},
            ]
            \addplot[samples=100, domain=0:2*pi, red] 
            ({cos(deg(x))}, {sin(deg(x))}); 
            \addlegendentry{test}
        \end{axis}
    \end{tikzpicture}
\end{document}

The output is:

Fully translucent legend

As you can see the outline is fully drawn, but the text inside the legend is also transparent. How can avoid this, and only make the fill translucent?

9

You can add the key

text opacity =1

to your legend style command.

screenshot

% arara: pdflatex
\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=5cm,
            axis equal,
            legend style={fill=white, fill opacity=0.6, draw opacity=1,text opacity=1},
            ]
            \addplot[samples=100, domain=0:2*pi, red] 
            ({cos(deg(x))}, {sin(deg(x))}); 
            \addlegendentry{test}
        \end{axis}
    \end{tikzpicture}
\end{document}`
  • Now, that was really too easy :-). Stupid me. Thanks! – Ingo May 17 '14 at 15:03
  • @Ingo no worries :) – cmhughes May 17 '14 at 15:04
  • That's great. One more question: how can I put the legend behind the red curve? Thanks! – Fabio Jul 4 '14 at 11:57
  • Note that you could also set fill=none to achieve a similar effect. – Felix Jul 12 '16 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.