![pgfplots:隐藏轴不连续处的网格线](https://linux22.com/image/454024/pgfplots%EF%BC%9A%E9%9A%90%E8%97%8F%E8%BD%B4%E4%B8%8D%E8%BF%9E%E7%BB%AD%E5%A4%84%E7%9A%84%E7%BD%91%E6%A0%BC%E7%BA%BF.png)
合并时axis y discontinuity=crunch
,grid
如下图所示,网格线在不连续处可见,参见(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}
答案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}