4

I was wondering how you would plot crosses at certain points on a graph using TikZ. I have managed to plot circles using the code

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

(I'm sure there is a much simpler way but I am new to TikZ) however, I have not been able to do something similar with crosses.

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Aug 6 '14 at 20:16
  • 1
    Also, check out page 332 of the manual. – John Kormylo Aug 6 '14 at 20:18
3

An option using pics:

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

enter image description here

If this is for some kind of plot, pgfplots could be a better choice:

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

enter image description here

2

If you don't need your marks to be like nodes, you can use plot coordinates command.

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

enter image description here

1

Simply to get the idea I only modified 2 lines of your code to produce red crosses on the right axis. You can repeat as you wish.

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

The output is:

enter image description here

\end{document}

0

Try

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

The output is closer to what you're looking for:

enter image description here

  • I tried that but for some reason, the crosses don't show up on my plot. I have edited the code to include more detail if this helps. – user60327 Aug 6 '14 at 22:00
  • Also, including the circle overlaps with the axis, making it appear as if there are gaps. I know this is picky but I need it to be just a cross if possible? Thanks – user60327 Aug 6 '14 at 22:09
  • @user60327 I changed the code now that I understand better what you're trying to do. Hope it works for you. – AndyS Aug 7 '14 at 4:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.