4

How to draw (major) grid lines only at specified positions, such as x=0, x=2 in the following MWE@ShareLaTeX (readonly now)?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.13}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    xlabel={$x$},
    ylabel={$y$},
    xtick={-4,-2,...,4},
    grid=major,  % what aboud only at x=0 and x=2?
  ]
    \addplot {x^2};
  \end{axis}
\end{tikzpicture}
\end{document}
hengxin
  • 2,371

1 Answers1

7

You mean something like the following? (Have a look at the comments in the code to find out how it works.)

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xlabel={$x$},
        ylabel={$y$},
        xtick={-4,-2,...,4},
        % place some extra x ticks ...
        extra x ticks={0,2},
        % ... but do not repeat the already existent labels
        % (from the "normal" ticks) ...
        extra x tick labels={},
        % ... and set the style of the extra ticks to `major'
        extra tick style={
            grid=major,
        },
    ]
        \addplot {x^2};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535