Pgfplots 网格线终止于整数边界

Pgfplots 网格线终止于整数边界

是否有一种pgfplots样式可以帮助我让网格线停止在整数边界处而不是从xmin ... xmax(和ymin ... ymax)延伸?

所需的输出显示在右侧,它使用自定义宏来绘制网格):

在此处输入图片描述

笔记:

  • 不需要自动化解决方案——明确提供网格的xmin ... xmax(和ymin ... ymax) 就可以了。似乎我可以为网格和轴设置不同的xmin ... xmax(和) 来实现这一点。ymin ... ymax

代码:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\newcommand*{\MyPgfplotsGrid}[4]{%
    \foreach \xCoord in {#1,...,#2} {% Vertical Grid Lines
        \addplot [mark=none, forget plot, help lines] coordinates {(\xCoord,#3) (\xCoord,#4)};
    }%
    \foreach \yCoord in {#3,...,#4} {% Horizontal Grid Lines
        \addplot [mark=none, forget plot, help lines] coordinates {(#1,\yCoord) (#2,\yCoord)};
    }%
}%

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmajorgrids=true,
    ymajorgrids=true,
    yminorgrids=true,
    minor tick num=1,
    axis lines=middle,
    xmin=-3.5, xmax=3.5,
    ymin=-3.5, ymax=3.5,
]
\end{axis}
\end{tikzpicture}
\quad
\begin{tikzpicture}
\begin{axis}[
    %xmajorgrids=true,
    %ymajorgrids=true,
    %yminorgrids=true,
    minor tick num=1,
    axis lines=middle,
    xmin=-3.5, xmax=3.5,
    ymin=-3.5, ymax=3.5,
]
    \MyPgfplotsGrid{-3}{3}{-3}{3}%
\end{axis}
\end{tikzpicture}
\end{document}

答案1

有点粗糙,但你可以手动绘制轴线,

execute at end axis={
         \draw [-stealth] (axis cs:-3.5,0) -- (axis cs:3.5,0);
         \draw [-stealth] (axis cs:0,-3.5) -- (axis cs:0,3.5);
   }

clip mode=individual同样需要,并axis line style={draw=none}关闭绘制“真实”轴。

当然,您可以将所有设置包装在一种样式中,以减少一些混乱。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{MyGrid/.style={xmin=-#1,xmax=#1,ymin=-#1,ymax=#1,
%            xtick={-#1,...,#1},ytick={-#1,...,#1},  % don't know if you want this
             xmajorgrids=true,ymajorgrids=true,
             yminorgrids=true,minor tick num=1,
             axis lines=middle,
             axis line style={draw=none},
             clip mode=individual,
             execute at end axis={
               \draw [-stealth] (axis cs:-#1-0.5,0) -- (axis cs:#1+0.5,0);
               \draw [-stealth] (axis cs:0,-#1-0.5) -- (axis cs:0,#1+0.5);}
             }}
\begin{document}
\begin{tikzpicture}
\begin{axis}[MyGrid=3]
\end{axis}
\end{tikzpicture}
\end{document}

要单独指定xmin/ xmax/ ymin/ ymax,可以使用style n args定义一个带有四个参数的样式。必须指定所有四个参数。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{MyGrid/.style n args={4}{xmin=-#1,xmax=#2,ymin=-#3,ymax=#4,
             xtick={-#1,...,#2},ytick={-#3,...,#4}, 
             xmajorgrids=true,ymajorgrids=true,
             axis lines=middle,
             axis line style={draw=none},
             clip mode=individual,
             execute at end axis={
               \draw [-stealth] (axis cs:-#1-0.5,0) -- (axis cs:#2+0.5,0);
               \draw [-stealth] (axis cs:0,-#3-0.5) -- (axis cs:0,#4+0.5);}
             }}
\begin{document}
\begin{tikzpicture}
\begin{axis}[MyGrid={3}{2}{1}{4}]
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容