2

I'm using the following code to generate a simple graph, and I want to have the legend show a filled in gray square to represent the shaded area on the graph. Everything still leads to a white square mark.

    \begin{tikzpicture}
\begin{axis}[
    inner axis line style={>={Latex[round]}},
    axis lines=left,
    ymin=0,
    ymax=10,
    xmin=0,
    xmax=10,
    yticklabels={,,},
    xticklabels={,,},
    ticks=none,
    xlabel=Quantity (q),
    ylabel=Price (\$),
    legend entries={
        total willingness to pay,
        marginal willingness to pay
    },
    legend pos=north east,
    legend style={draw=none}
    ]
    \addlegendimage{only marks, mark=square}
    \addlegendimage{only marks, mark=o}
    \addplot[solid,domain=0:10,samples=100]{-5/2*x^(1/2)+15/2};
    \addplot[draw=none,name path=A,domain=3:6,fill=gray]{-5/2*x^(1/2)+15/2}\closedcycle;    
\end{axis}
\end{tikzpicture}

Thank you for your help!

  • 1
    @Jake, oh, with that short answer you were way quicker than I was ;) – Stefan Pinnow Feb 18 '16 at 16:05
  • @Jake Thank you both so much for your help! That simple fix was taking so much time to figure out on my own – Zachary Grzelka Feb 18 '16 at 16:09
4

You have to use mark=square* instead of mark=square to fill it. With color you can than fill it with a color of your choice.

\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    inner axis line style={>={Latex[round]}},
    axis lines=left,
    ymin=0,
    ymax=10,
    xmin=0,
    xmax=10,
    yticklabels={,,},
    xticklabels={,,},
    ticks=none,
    xlabel=Quantity (q),
    ylabel=Price (\$),
    legend entries={
        total willingness to pay,
        marginal willingness to pay
    },
    legend pos=north east,
    legend style={draw=none}
    ]
    \addlegendimage{only marks, mark=square*,color=gray}
    \addlegendimage{only marks, mark=o}
    \addplot[solid,domain=0:10,samples=100]{-5/2*x^(1/2)+15/2};
    \addplot[draw=none,domain=3:6,fill=gray]{-5/2*x^(1/2)+15/2}\closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.