[Tex/LaTex] Add a legend entry describing the area of a “fill” plot

legendpgfplots

I have a plot for which I want to add a legend entry describing an "area plot", but \addlegendentry isn't doing the trick. Does anyone have a solution for this?

I give you a small extract of my code and the image which I'm currently getting:

\begin{figure}[h]
\begin{center}
\begin{tikzpicture}[scale = 1, baseline, domain = 0:220]
    \begin{axis}[
        axis lines = left,
        xlabel = {$Q_d$},
        ylabel = {$P_x$},
        x label style={at={(axis description cs:1,0)},anchor=east},
        y label style={at={(axis description cs:0,1)},rotate=270,anchor=south},
        xtick = {70,190},
        ytick = {50,120,190},
        ymax= 200, ymin =0,
        xmax= 230, xmin = 0,
        legend style={at={(0.4,-0.15)},anchor=north},
        legend columns = -1]
        \addplot[fill=green, fill opacity = 0.2, draw = none] coordinates {(0,190) (0,120) (70,120)};
        \addlegendentry{Excedente consumidor};
        \addplot[fill=blue, fill opacity = 0.2, draw = none] coordinates {(0,120) (0,50) (70,120)};
        \addlegendentry{Excedente productor}; 
        \addplot[color = black]{(-1)*x + 190};
        \addplot[color = black, domain = 0:190]{x + 50};
        \draw[black!30, dashed] (axis cs: 0,120) to (axis cs:70,120);
        \draw[black!30, dashed] (axis cs:70,0) to (axis cs:70,120);
        \filldraw [fill=yellow, draw=black, thick] (axis cs:70,120) circle [radius=2pt];
    \end{axis}
\end{tikzpicture}
\end{center}
\caption{Pregunta 7}
\label{preg7lab6}
\end{figure}

PS I think I can't add a picture because it is my first question here. (edit)

Best Answer

Is this what you want? The only change I made was to add area legend to the two \addplot commands.

enter image description here

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[scale = 1, baseline, domain = 0:220]
    \begin{axis}[
        axis lines = left,
        xlabel = {$Q_d$},
        ylabel = {$P_x$},
        x label style={at={(axis description cs:1,0)},anchor=east},
        y label style={at={(axis description cs:0,1)},rotate=270,anchor=south},
        xtick = {70,190},
        ytick = {50,120,190},
        ymax= 200, ymin =0,
        xmax= 230, xmin = 0,
        legend style={at={(0.4,-0.15)},anchor=north},
        legend columns = -1]
        \addplot[fill=green, fill opacity = 0.2, draw = none,area legend] coordinates {(0,190) (0,120) (70,120)};
        \addlegendentry{Excedente consumidor};
        \addplot[fill=blue, fill opacity = 0.2, draw = none,area legend] coordinates {(0,120) (0,50) (70,120)};
        \addlegendentry{Excedente productor}; 
        \addplot[color = black]{(-1)*x + 190};
        \addplot[color = black, domain = 0:190]{x + 50};
        \draw[black!30, dashed] (axis cs: 0,120) to (axis cs:70,120);
        \draw[black!30, dashed] (axis cs:70,0) to (axis cs:70,120);
        \filldraw [fill=yellow, draw=black, thick] (axis cs:70,120) circle [radius=2pt];
    \end{axis}
\end{tikzpicture}
\caption{Pregunta 7}
\label{preg7lab6}
\end{figure}
\end{document}
Related Question