我有一个带有通过/失败指标的图表,希望通过在图表中分离颜色来清楚地显示这一点。为了使数据突出,我在轴刻度标记处有一个背景网格。然而,这并不影响我用来为图表下方区域着色的填充,因为这似乎是顶层。
我现在拥有的一个例子如下所示:
用于生成该代码的代码是:
\documentclass{article}
\usepackage{pgfplots, amsmath}
\pgfplotsset{compat=newest}
\pgfplotsset{major grid style={color=black}}
\pgfplotsset{every tick/.style={black,}}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=5,
xmax=55,
ymin=0,
tick align=outside,
tickpos=left,
xtick=data,
ytick={0,0.2,...,2.5},
enlargelimits=false,
axis background/.style={fill=green!30},
grid=both,
xlabel=Frequency (MHz),
ylabel=V$_{\text{DD}}$ (V)]
\addplot
[const plot, fill=red!30]
coordinates
{
(5,0.7)
(10,0.8)
(15,0.9)
(20,1)
(25,1.1)
(30,1.2)
(35,1.3)
(40,1.4)
(45,1.5)
(50,1.6)
(55,1.7)
}
\closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}
我希望网格线与整个图重叠(也意味着填充)。我该如何实现?
答案1
可以通过设置在图的顶部绘制网格和轴axis on top
:
\documentclass{article}
\usepackage{pgfplots, amsmath}
\pgfplotsset{compat=newest}
\pgfplotsset{
major grid style=black,
tick style=black
}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=5,
xmax=55,
ymin=0,
tick align=outside,
tickpos=left,
xtick=data,
ytick={0,0.2,...,2.5},
enlargelimits=false,
axis background/.style={fill=green!30},
grid=both,
xlabel=Frequency (MHz),
ylabel=V$_{\text{DD}}$ (V),
axis on top
]
\addplot
[const plot, fill=red!30]
coordinates
{
(5,0.7)
(10,0.8)
(15,0.9)
(20,1)
(25,1.1)
(30,1.2)
(35,1.3)
(40,1.4)
(45,1.5)
(50,1.6)
(55,1.7)
}
\closedcycle;
\end{axis}
\end{tikzpicture}
\end{document}