2

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

1
  • 1
    The easy solution is to draw a [fill=white] band acroo the page, or draw the gird outside the axis environment with clipping. Upi would need to record all the end points. Aug 23, 2022 at 20:10

3 Answers 3

1

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

2

Inspired by John's idea:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis y discontinuity=crunch,
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        set layers,
        grid,
        ]
        \begin{pgfonlayer}{axis grid}
            \draw[white] (0,0) -- (0,1);
            \draw[white] (1,0) -- (1,1);
        \end{pgfonlayer}
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • 1
    +1 - One of the solutions I also looked at. Your white lines have the same width as default grid lines, so it is safer to make them thick, so there are no viewer artefacts. -and by using axis description cs: it will be independent of xmin and xmax like this: \draw[white, thick] (axis description cs:0,0) -- (axis description cs:0,1) (axis description cs:1,0) -- (axis description cs:1,1); Aug 23, 2022 at 23:47
  • Ok - maybe thick is not optimal, as it cuts into the start of the horizontal grid lines. -so then the white lines should not be drawn all the way, but only up to below the first horizontal grid line. Aug 23, 2022 at 23:53
2

Not elegant, but it works.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\standaloneenv{tikzpicture}% redundant
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis y discontinuity=crunch,
        xmin=0, xmax=1,
        ymin=0, ymax=1,
        name=border
        ]
      \addplot[domain=0:1, red] {x};
    \end{axis}
% Note (border.south west)=(0,0)
    \path (border.north east);
    \pgfgetlastxy{\xs}{\ys}%
    \begin{scope}[even odd rule, on background layer]
      \clip (1pt,0) rectangle (\xs-1pt, \ys)
        (1pt, 0.03*\ys) rectangle (\xs-1pt, 0.11*\ys);% by eyeball
      \draw[help lines] (0,0) grid[xstep={\xs/5}, ystep={\ys/5}] (\xs, \ys);
    \end{scope}
\end{tikzpicture}
\end{document}

demo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .