Pgfplots 网格不显示

Pgfplots 网格不显示

我在使用 为此图设置网格 时遇到了麻烦pgfplots

在此处输入图片描述

这是我的代码。

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[
    /pgf/declare function = {
        signal = 0.8 * (sin(deg(x)) + 0.7*sin(2*deg(x)) + 0.7*sin(3*deg(x)));
    }
]

\begin{axis}[
    grid = minor,
    height= 0.35\textwidth,
    width = 0.8\textwidth,
    thick,
    scale=1.8,
    axis lines=center,
    domain=0:20*pi,
    samples=500,
    line join=bevel,
    xlabel=t,
    ymax=2.7,
    ymin=-2.7,
    xmax=25.9,
    xmin=0,
    xtick=\empty,
    ytick=\empty,
] 
\addplot[ultra thick,black!40!cyan] { signal };

\end{axis}
\end{tikzpicture}
\end{document}

这个问题和轴线的设置有关系吗?

答案1

如果您想查看网格,请不要说xtick=\emptyand/or ytick=\empty。使用xticklabels=\emptyandyticklabels=\empty不显示ticklabels,使用major thick length=0andminor thick length=0不显示刻度。

您只想查看次要网格?还是两者都要查看?对于次要网格,您必须设置minor tick num。在下面的示例中,我同时使用 和 ,minor thick num=3并将颜色设为major grid红色。

在此处输入图片描述

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[
    /pgf/declare function = {
        signal = 0.8 * (sin(deg(x)) + 0.7*sin(2*deg(x)) + 0.7*sin(3*deg(x)));
    }
]

\begin{axis}[
    grid = both,
    minor tick num=3,
    every major grid/.style={red, opacity=0.5},
    height= 0.35\textwidth,
    width = 0.8\textwidth,
    thick,
    scale=1.8,
    axis lines=center,
    domain=0:20*pi,
    samples=500,
    line join=bevel,
    xlabel=t,
    ymax=2.7,
    ymin=-2.7,
    xmax=25.9,
    xmin=0,
    xticklabels=\empty,
    yticklabels=\empty,
    major tick length=0pt,
    minor tick length=0pt,
    %xtick=\empty,
    %ytick=\empty,
] 
\addplot[ultra thick,black!40!cyan] { signal };

\end{axis}
\end{tikzpicture}
\end{document}

相关内容