3

I would simply like to change the legend entries from being lines to a green rectangle for the first entry of the legend and a green rectangle for the second: I am using pgfplot and fillbetween, I want the same legend output as in this question, but with the same trick does not work in my case (maybe I didn't understand something)

enter image description here

\documentclass[12pt]{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=9cm,compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{figure}[t]
\centering
\begin{tikzpicture}
\begin{axis}[
title = {$\delta = 0.9$},
xlabel = {$\xi$},
ylabel = {$c(\xi)$},
xmin = 0,
xmax = 1,
ymin = 0.17,
ymax=  1,
legend pos = south east,
]
\addplot[
name path = A,
domain= 0:1,
samples= 100,
line width = 1.2 pt, 
]
{(1+x)/2};
\node at (axis cs:  .8,  .84){\footnotesize$C(\xi)$};
\addplot[
name path  = B,
domain= 0:1,
samples= 100,
style = dashed,
]
{(1/4 + (0.9 +x)/2};
\node at (axis cs: 0.2,0.87){\footnotesize$\bar{c}(\xi;\delta)$};
\addplot[
name path = C,
domain= 0:1,
samples= 100,
style = dashed,]
{(3/4 + (-0.9 +x)/2};
\node at (axis cs:  .8,  .63){\footnotesize$\underline{c}(\xi;\delta)$};
\addplot[fill=green, fill opacity = 0.3, draw = none,area legend] fill between [of = A and C, soft clip={domain=0:1}];
\addlegendentry{$Q_{ABS} < Q_{S,PC}$};
\addplot[fill=red, fill opacity = 0.3, draw = none,area legend] fill between [of = B and A, soft clip={domain=0:1}];
\addlegendentry{$Q_{ABS} > Q_{U,PC}$};
\end{axis}
\end{tikzpicture}
\label{fig:SustProf}
\caption{Some Caption}
\end{figure}
\end{document}

2 Answers 2

3

Try this code using \addlegendimage{area legend, ...} to put the rectangles in the legend area.

c

\documentclass[12pt]{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=9cm,compat=1.10}
\usepgfplotslibrary{fillbetween}

\begin{document}
    \begin{figure}[t]
        \centering
        \begin{tikzpicture}
            \begin{axis}[
                title = {$\delta = 0.9$},
                xlabel = {$\xi$},
                ylabel = {$c(\xi)$},
                xmin = 0,
                xmax = 1,
                ymin = 0.17,
                ymax=  1,
                legend pos = south east,    
                ]
                \addlegendimage{area legend, fill=green, fill opacity = 0.3}                  
                \addlegendimage{area legend, fill=red, fill opacity = 0.3}
                \addplot[
                name path = A,
                domain= 0:1,
                samples= 100,
                line width = 1.2 pt, 
                ]
                {(1+x)/2};
                \node at (axis cs:  .8,  .84){\footnotesize$C(\xi)$};
                \addplot[
                name path  = B,
                domain= 0:1,
                samples= 100,
                style = dashed,
                ]
                {(1/4 + (0.9 +x)/2};
                \node at (axis cs: 0.2,0.87){\footnotesize$\bar{c}(\xi;\delta)$};
                \addplot[
                name path = C,
                domain= 0:1,
                samples= 100,
                style = dashed,]
                {(3/4 + (-0.9 +x)/2};
                \node at (axis cs:  .8,  .63){\footnotesize$\underline{c}(\xi;\delta)$};
                
                \addplot[fill=green, fill opacity = 0.3, draw = none] fill between [of = A and C, soft clip={domain=0:1}];

                \addplot[fill=red, fill opacity = 0.3, draw = none] fill between [of = B and A, soft clip={domain=0:1}];
                
                \addlegendentry{$Q_{ABS} < Q_{S,PC}$};                  
                \addlegendentry{$Q_{ABS} > Q_{S,PC}$};

            \end{axis}
        \end{tikzpicture}
        \label{fig:SustProf}
        \caption{Some Caption}
    \end{figure}
\end{document}

The code could be easier to read using

...
                legend pos = south east,
                legend entries={$Q_{ABS} < Q_{S,PC}$,   $Q_{ABS} > Q_{S,PC}$},          
                ]
                \addlegendimage{area legend, fill=green}                  
                \addlegendimage{area legend, fill=red}

....

instead of \addlegendentry

4

Every \addlegendentry command adds an entry to the legend starting from the first \addplot command. It is irrelevant where you place the \addlegendentry command (it must be added after the relevant \addplot command, of course). Thus, with your code you add two legend entries that refer to the first and the second \addplot command respectively and not to the last two. Since you did not provide the option area legend to the first two \addplot commands (which is totally correct, because you don't want to add a legend entry for these plots anyways), you will get (dashed or thick) lines as legend picture.

You can verify this behaviour by adding some more \addlegendentry commands to your code. You will see that the legend pictures will have the same styling as the respective \addplot macros:

enter image description here

So, what you essentially need to do is to skip the first three \addplot commands so that legend entries will only be added for the last two \addplot commands. You can use the option forget plot to achieve this (see also this answer).

Having added the option forget plot to the first three \addplot commands, the correct legend images will magically appear:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=9cm, compat=1.10}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{figure}[t]
\centering
\begin{tikzpicture}
\begin{axis}[
title = {$\delta = 0.9$},
xlabel = {$\xi$},
ylabel = {$c(\xi)$},
xmin = 0,
xmax = 1,
ymin = 0.17,
ymax=  1,
legend pos = south east,
]
\addplot[
name path = A,
domain= 0:1,
samples= 100,
line width = 1.2 pt, 
forget plot
]
{(1+x)/2};
\node at (axis cs:  .8,  .84){\footnotesize$C(\xi)$};
\addplot[
name path  = B,
domain= 0:1,
samples= 100,
style = dashed,
forget plot
]
{(1/4 + (0.9 +x)/2};
\node at (axis cs: 0.2,0.87){\footnotesize$\bar{c}(\xi;\delta)$};
\addplot[
name path = C,
domain= 0:1,
samples= 100,
style = dashed,
forget plot
]
{(3/4 + (-0.9 +x)/2};
\node at (axis cs:  .8,  .63){\footnotesize$\underline{c}(\xi;\delta)$};
\addplot[fill=green, fill opacity = 0.3, draw = none, area legend] fill between [of = A and C, soft clip={domain=0:1}];
\addplot[fill=red, fill opacity = 0.3, draw = none, area legend] fill between [of = B and A, soft clip={domain=0:1}];
\addlegendentry{$Q_{ABS} < Q_{S,PC}$}
\addlegendentry{$Q_{ABS} > Q_{U,PC}$}
\end{axis}
\end{tikzpicture}
\label{fig:SustProf}
\caption{Some Caption}
\end{figure}
\end{document}

enter image description here

In certain cases, forget plot might lead to unwanted side effects, because it does not only remove the plot from the legend. You can also put empty legend entries instead using \legend{,,,$Q_{ABS} < Q_{S,PC}$,$Q_{ABS} > Q_{U,PC}$};:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=9cm, compat=1.10}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{figure}[t]
\centering
\begin{tikzpicture}
\begin{axis}[
title = {$\delta = 0.9$},
xlabel = {$\xi$},
ylabel = {$c(\xi)$},
xmin = 0,
xmax = 1,
ymin = 0.17,
ymax=  1,
legend pos = south east,
]
\addplot[
name path = A,
domain= 0:1,
samples= 100,
line width = 1.2 pt, 
]
{(1+x)/2};
\node at (axis cs:  .8,  .84){\footnotesize$C(\xi)$};
\addplot[
name path  = B,
domain= 0:1,
samples= 100,
style = dashed,
]
{(1/4 + (0.9 +x)/2};
\node at (axis cs: 0.2,0.87){\footnotesize$\bar{c}(\xi;\delta)$};
\addplot[
name path = C,
domain= 0:1,
samples= 100,
style = dashed,
]
{(3/4 + (-0.9 +x)/2};
\node at (axis cs:  .8,  .63){\footnotesize$\underline{c}(\xi;\delta)$};
\addplot[fill=green, fill opacity = 0.3, draw = none, area legend] fill between [of = A and C, soft clip={domain=0:1}];
\addplot[fill=red, fill opacity = 0.3, draw = none, area legend] fill between [of = B and A, soft clip={domain=0:1}];
\legend{,,,$Q_{ABS} < Q_{S,PC}$,$Q_{ABS} > Q_{U,PC}$};
\end{axis}
\end{tikzpicture}
\label{fig:SustProf}
\caption{Some Caption}
\end{figure}
\end{document}

The output is the same as above.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .