9

I need to add an extra tick to the x-axis without adding a new grid line at the new x value, the problem is that I'm using extra ticks to get a complete grid (it is one solution that was given to me in this post) wich lines start and end at integer boundaries of the x and y-axis.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=center,
    grid=major,
    grid style={dotted, cyan},
    anchor=origin,
    xlabel = $x$, ylabel =$y$,  % Set the labels
    xmin= -4, xmax= 4,
    ymin=-4, ymax=4,
    xtick={-3,-2,...,3},
    ytick={-3,-2,...,3},
    extra x ticks={-4,3.14, 4},
    extra y ticks={-4, 4},
    extra tick style={grid=major, grid style={dotted, cyan}},
    extra x tick labels={,\hspace{.4em}$\pi$,},
    extra y tick labels={},
    color=cyan
]
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • I need to do the inverse thing: Keep all grid lines and erase the vertical line which passes through the pi abscissa.
    – Hamed
    Sep 24, 2015 at 23:39

1 Answer 1

9

To add extra tick in the x axis, you can use

extra x tick style={grid=none},

in the options:

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=center,
    grid=major,
    grid style={dotted, cyan},
    anchor=origin,
    xlabel = $x$, ylabel =$y$,  % Set the labels
    xmin= -4, xmax= 4,
    ymin=-4, ymax=4,
    xtick={-3,-2,...,3},
    ytick={-3,-2,...,3},
    extra x ticks={-4,3.14, 4},
    extra y ticks={-4, 4},
    extra tick style={grid=major, grid style={dotted, cyan}},
    extra x tick labels={,\hspace{.4em}$\pi$,},
    extra y tick labels={},
    extra x tick style={grid=none}, %% <<-----
    color=cyan
]
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

This will make the last grid lines on either side of x axis to disapper which can be drawn using

\draw[dotted,cyan] (4,-4) -- (4,4)
                   (-4,-4) -- (-4,4);
\end{axis}

enter image description here

Here is the manually drawing option mentioned by Gonzalo:

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines=center,
    grid=major,
    grid style={dotted, cyan},
    anchor=origin,
    xlabel = $x$, ylabel =$y$,  % Set the labels
    xmin= -4, xmax= 4,
    ymin=-4, ymax=4,
    xtick={-3,-2,...,3},
    ytick={-3,-2,...,3},
    extra x ticks={-4, 4},
    extra y ticks={-4, 4},
    extra tick style={grid=major, grid style={dotted, cyan}},
    extra x tick labels={},%,\hspace{.4em}$\pi$,},
    extra y tick labels={},
    %extra x tick style={grid=none}, %% <<-----
    color=cyan
]
 \draw[very thin,gray] (3.14,0.1) -- (3.14,-0.1)node[anchor=north west,inner xsep=0pt,text=cyan] {$\pi$};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
  • Isn't it simpler to add the tick manually? Sep 24, 2015 at 23:42
  • Yes, I will add that option too in some time. It is jogging time now. :)
    – user11232
    Sep 24, 2015 at 23:49
  • Changing the thickness or the line type to densely dotted or heavy dashed may lead to some difference between lines.
    – Hamed
    Sep 25, 2015 at 0:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .