网格步骤相对位置tikz

网格步骤相对位置tikz

我发现网格的步长不是相对于网格被调用的位置,而是相对于原点。因此,在像这样的图中,网格会相对于多个轴移动。有什么技巧可以让它相对于它被调用的位置,或者需要计算偏移\pgfmathsetmacro{\myshiftX}{calculations} 并将其作为参数提供\draw[step=0.25,xshift=\myshiftX]

\documentclass[tikz]{standalone}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[node distance=3mm, scale=1.2]
\def\lengthAxis{1.1}
\def\AxisSpace{2.4}

\foreach \elem [count=\i] in {1,2,3,4,5,6,7,8}{
\pgfmathsetmacro{\x}{\AxisSpace*mod(\i-1,4)}
\pgfmathsetmacro{\y}{-1*\AxisSpace*int((\i-1)/4)}

\draw[step=0.25,gray,very thin] (\x,\y) +(-1.05,-1.05) grid +(1.05,1.05);

\path (\x,\y)   + (          \lengthAxis,                         0)    coordinate (meanI)
                        + (  -1*\lengthAxis,                          0)    coordinate (medI) 
                        + (                       0,        \lengthAxis )   coordinate (minI) 
                        + (                           0,  -1*\lengthAxis)   coordinate (maxI);

\draw[thin] (meanI) -- (medI) (minI) -- (maxI);

\draw[thin] (\x,\y) + (    1,      0)   circle [radius=1pt] node[below] {1}
                                + (   -1,      0)   circle [radius=1pt] node[above] {1}
                                + (      0,     1)  circle [radius=1pt] node[right]         {1}
                                + (        0,     -1)   circle [radius=1pt] node[left]      {1}
                                + ( 0.5,       0)   circle [radius=1pt] node[below] {0.5}
                                + ( -0.5,      0)   circle [radius=1pt] node[above] {0.5}
                                + (    0,   0.5)    circle [radius=1pt] node[right]         {0.5}
                                + (    0, -0.5) circle [radius=1pt] node[left]      {0.5};

}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

正如评论中所说,你的情况最简单的方法可能就是使用

\draw[step=0.25,gray,very thin, shift={(\x,\y)}] (-1.05,-1.05) grid (1.05,1.05);

它将整个图像沿着矢量移动,并简单地从到(\x, \y)绘制一个网格。(-1.05,-1.05)(1.05,1.05)


您可能还倾向于\matrix在此处使用。

代码

\documentclass[tikz]{standalone}
\tikzset{
  every myGrid picture/.style={scale=1.2},
  every myGrid grid/.style={step=0.25, help lines, draw},
  every myGrid ticks/.style={radius=+1pt, draw, nodes={font=\tiny}},
  every myGrid axes/.style=draw,
  myGrid grid/.initial=1.05,
  myGrid axis/.initial=1.1,
}
\newcommand*\myGrid[1][]{%
\scope[every myGrid picture/.try, #1]
  \path[every myGrid grid/.try]
    (-\pgfkeysvalueof{/tikz/myGrid grid}, -\pgfkeysvalueof{/tikz/myGrid grid}) grid
    ( \pgfkeysvalueof{/tikz/myGrid grid},  \pgfkeysvalueof{/tikz/myGrid grid});
  \path[every myGrid ticks/.try]
    \foreach \sign/\val in {-/1, -/0.5, /0.5, /1} {
     (right:\sign\val) circle[]
       node[style/.expanded={\if\sign-above\else below\fi}] {\val}
     (up:   \sign\val) circle[]
       node[style/.expanded={\if\sign-left\else right\fi}]  {\val}
   };
  \path[every myGrid axes/.try]
   (down:\pgfkeysvalueof{/tikz/myGrid axis})--(   up:\pgfkeysvalueof{/tikz/myGrid axis})
   (left:\pgfkeysvalueof{/tikz/myGrid axis})--(right:\pgfkeysvalueof{/tikz/myGrid axis});
\endscope
}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\matrix [
  column sep={1.2*2.4cm,between origins},
  row sep={1.2*2.4cm,between origins}
]{
 \myGrid & \myGrid & \myGrid & \myGrid \\
 \myGrid & \myGrid & \myGrid & \myGrid[every myGrid grid/.append style={step=.1}] \\
};
\end{tikzpicture}

\begin{tikzpicture}\def\AxisSpace{2.4}
\foreach \elem [evaluate={\xVal=\AxisSpace*mod(\elem,4)},
                evaluate={\yVal=-\AxisSpace*int(\elem/4)}] in {0,...,7}{
  \myGrid[shift={(\xVal, \yVal)}]
}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

在此处输入图片描述

相关内容