在 tikz 中的图表上绘制十字线

在 tikz 中的图表上绘制十字线

我想知道如何使用 TikZ 在图表上的某些点绘制十字。我已经使用代码绘制了圆圈

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
\draw[step=1cm,gray,dotted] (-5.9,-1.9) grid (5.9,1.9);
\draw[semithick] (-6,0) -- (6,0);
\draw[semithick] (0,-2) -- (0,2);

\foreach \x in {-5,-4,-3,-2,-1,1,2,3,4,5}
    \draw (\x cm, 1.5pt) -- (\x cm, -1.5pt) node[anchor=north] {\scriptsize $\x$};
\fill [radius=2.5pt,color=blue] (-5.2,0) circle[] (-4.2,0) circle[] (-3.2,0) circle[] (-2.2,0) circle[] (-1.2,0) circle[] (-0.2,0) circle;
\fill [radius=2.5pt,color=red] (0.1,0) circle[] (1.1,0) circle[] (2.1,0) circle[] (3.1,0) circle[] (4.1,0) circle[] (5.1,0) circle;
\end{tikzpicture}

\end{document}

(我确信有一种更简单的方法,但我对 TikZ 还不熟悉)然而,我还不能用十字架做类似的事情。

答案1

使用 s 的选项pic

\documentclass[tikz]{standalone}

\tikzset{
  mycross/.pic={
    \draw[pic actions] 
      (-3pt,0) -- (3pt,0)
      (0,-3pt) -- (0,3pt);
  },
}

\begin{document}

\begin{tikzpicture}
\draw[step=1cm,gray,dotted] (-5.9,-1.9) grid (5.9,1.9);
\draw[semithick] (-6,0) -- (6,0);
\draw[semithick] (0,-2) -- (0,2);

\foreach \x in {-5,-4,-3,-2,-1,1,2,3,4,5}
{
  \draw 
    (\x cm, 1.5pt) -- (\x cm, -1.5pt) node[anchor=north] {\scriptsize $\x$};
  \pic at (\x,0.5) {mycross}; 
  \pic[line width=1pt,red] at (\x,1) {mycross}; 
  \pic[line width=1.5pt,cyan] at (\x,-1) {mycross}; 
}
\end{tikzpicture}

\end{document}

在此处输入图片描述

如果这是为了某种情节,pgfplots可能是更好的选择:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
ymin=-2,
ymax=2,
xmin=-5,
xmax=5,
width=\textwidth,
grid=major,
major grid style=dotted
]
\addplot+[
  mark=+,
  only marks,
  mark size=6pt,
  mark options={line width=3pt}
] 
  coordinates
  {(-4.5,0) (-3,0) (-2,0) (-1,0) (0,0) (1,0) (2,0) (2.5,0) (4,0)};
\addplot+[
  mark=+,
  only marks,
  mark size=5pt,
  mark options={line width=2pt},
  mark color=red
] 
  coordinates
  {(-3,1.5) (-2,1.5) (-1,1.5) (0,1.5) (1,1.5) (2,1.5) (2.5,1.5)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

如果您不需要marks像那样nodes,您可以使用plot coordinates命令。

\documentclass[tikz]{standalone}
%\usetikzlibrary{plotmarks}

\begin{document}

\begin{tikzpicture}
\draw[step=1cm,gray,dotted] (-5.9,-1.9) grid (5.9,1.9);
\draw[semithick] (-6,0) -- (6,0);
\draw[semithick] (0,-2) -- (0,2);

\foreach \x in {-5,-4,-3,-2,-1,1,2,3,4,5}
    \draw (\x cm, 1.5pt) -- (\x cm, -1.5pt) node[anchor=north] {\scriptsize $\x$};

\draw plot[mark=*, mark options={color=red, scale=1.5}] coordinates { (-5.2,0) (-4.2,0) (-3.2,0) (-2.2,0) (-1.2,0) (-0.2,0)};

\draw plot[mark=+, mark options={color=blue, scale=1.5}] coordinates { (5.2,0) (4.2,0) (3.2,0) (2.2,0) (1.2,0) (0.2,0)};

\draw plot[mark=x, mark options={color=green, scale=1.5}] coordinates { (5.5,0) (4.5,0) (3.5,0) (2.5,0) (1.5,0) (0.5,0)};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

为了让您理解,我只修改了您的代码中的 2 行,以便在右轴上产生红色十字。您可以根据需要重复此操作。

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

\begin{tikzpicture}
\draw[step=1cm,gray,dotted] (-5.9,-1.9) grid (5.9,1.9);
\draw[semithick] (-6,0) -- (6,0);
\draw[semithick] (0,-2) -- (0,2);

\foreach \x in {-5,-4,-3,-2,-1,1,2,3,4,5}
    \draw (\x cm, 1.5pt) -- (\x cm, -1.5pt) node[anchor=north] {\scriptsize $\x$};
\fill [radius=2.5pt,color=blue] (-5.2,0) circle[] (-4.2,0) circle[] (-3.2,0) circle[] (-2.2,0) circle[] (-1.2,0) circle[] (-0.2,0) circle;

\foreach \x/\n in {0.1/a,1.1/b,2.1/c,3.1/d,4.1/e,5.1/f}{ % This is what I have added
    \node (\n)[circle,inner sep=2.5pt] at (\x,0){};
    \draw[red] (\n.north west)--(\n.south east)(\n.south west)--(\n.north east);
}
\end{tikzpicture}
\end{document}

输出为:

在此处输入图片描述

\结束{文档}

答案4

尝试

\documentclass{standalone}

\usepackage{tikz}

\tikzset{cross/.style={path picture={
  \draw[red]
    (path picture bounding box.south east)--(path picture bounding box.north west)
    (path picture bounding box.south west)--(path picture bounding box.north east);
}}}

\begin{document}

\begin{tikzpicture}
  \draw (-2,0)--(2,0);
  \foreach \x in {-1, 0, 1}{
    \node[cross,thick,minimum size=4pt] at (\x,0) {};
  }
\end{tikzpicture}

\end{document}

输出更接近您想要的:

在此处输入图片描述

相关内容