我正在使用以下代码生成一个简单的图表,并且我想让图例显示一个填充的灰色方块来表示图表上的阴影区域。一切仍然指向一个白色方块标记。
\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}