如何仅在指定位置绘制(主要)网格线?

如何仅在指定位置绘制(主要)网格线?

如何仅在指定位置绘制(主要)网格线,例如x=0, x=2下图MWE@ShareLaTeX(现在只读)

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

答案1

你是指下面这样的东西吗?(查看代码中的注释以了解它是如何工作的。)

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

该图显示了上述代码的结果

相关内容