Pgfplots: hide grid line in axis discontinuity

axispgfplots

When combining axis y discontinuity=crunch and grid as in the MWE below the grid line is visible in the discontinuity, cf. (0,0) -- (0,0.2) in the MWE. How can one avoid this without adjusting axis limits?


MWE:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\standaloneenv{tikzpicture}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis y discontinuity=crunch,
        xmin=0,
        ymin=0,
        grid,
        ]
    \end{axis}
\end{tikzpicture}
\end{document}

mwe result

Best Answer

If you do not use extra x ticks for anything else, then you can use them to not draw first and last grid lines like this:

\documentclass[tikz,border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\standaloneenv{tikzpicture}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis y discontinuity=crunch,
xmin=0, xmax=1,
ymin=0, ymax=1,
grid,
xtick={0.2,0.4,...,0.8},
extra x ticks={0,1},
extra x tick style={grid=none},
]
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here