这是我的 MWE:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
area style,
ymajorgrids=true,
bar width=0.6cm
]
\addplot+[ybar interval, mark=no, fill=black!20, draw=black!40] coordinates{(0.0,1) (0.75,1)
(0.75,1) (1.5,1)
(1.5,1) (2.25,1)
(2.25,0) (3.0,0)
(3.0,0) (3.75,0)
(3.75,2) (4.5,2)
(4.5,1) (5.25,1)
};
\end{axis}
\end{tikzpicture}
\end{document}
正如您在下面看到的,这些线条ymajorgrids
位于我绘图的“条”上方。
有没有什么办法可以防止这种情况发生?
谢谢
答案1
这是因为area legend
样式设置了axis on top
。要获得所需结果,只需将其删除并重命名mark=no
为mark=none
。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymajorgrids=true,
bar width=0.6cm,
]
\addplot+ [
mark=none,
ybar interval,
fill=black!20,
draw=black!40,
] coordinates{
(0.0,1) (0.75,1)
(0.75,1) (1.5,1)
(1.5,1) (2.25,1)
(2.25,0) (3.0,0)
(3.0,0) (3.75,0)
(3.75,2) (4.5,2)
(4.5,1) (5.25,1)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[set layers, %<-added
area style,
ymajorgrids=true,
bar width=0.6cm
]
\addplot+[ybar interval, mark=no, fill=black!20, draw=black!40,
on layer=axis foreground%<-added
] coordinates{(0.0,1) (0.75,1)
(0.75,1) (1.5,1)
(1.5,1) (2.25,1)
(2.25,0) (3.0,0)
(3.0,0) (3.75,0)
(3.75,2) (4.5,2)
(4.5,1) (5.25,1)
};
\end{axis}
\end{tikzpicture}
\end{document}