在圖例中填寫方框?

在圖例中填寫方框?

我使用以下程式碼產生一個簡單的圖表,我想讓圖例顯示一個填滿的灰色方塊來表示圖表上的陰影區域。一切仍然通往一個白色的方塊標記。

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

感謝您的幫忙!

答案1

你必須使用mark=square*而不是mark=square填充它。您可以用color您選擇的顏色填滿它。

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

顯示上述程式碼結果的圖像

相關內容