pgfplots:隐藏轴不连续处的网格线

pgfplots:隐藏轴不连续处的网格线

合并时axis y discontinuity=crunchgrid如下图所示,网格线在不连续处可见,参见(0,0) -- (0,0.2)图 2。如何在不调整轴限值的情况下避免这种情况?


梅威瑟:
\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 结果

答案1

如果您不将其extra x ticks用于其他任何用途,那么您可以使用它们来不绘制第一条和最后一条网格线,如下所示:

\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}

在此处输入图片描述

答案2

受到约翰的想法的启发:

\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}

在此处输入图片描述

答案3

虽然不优雅,但确实有效。

\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}

演示

相关内容