11

I'm having trouble with setting up a grid for this plot using pgfplots.

enter image description here

Here's my code.

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

Is this problem related to the setting of the axis lines?

  • 1
    Welcome to TeX.SE. Thanks for providing most of the required code. But, for future reference, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages. – Peter Grill Jun 5 '14 at 2:27
  • 1
    you could change your xtick=\empty to, for example, minor xtick={0,2,...,60}, – cmhughes Jun 5 '14 at 2:40
9

If you want to see the grid do not say xtick=\empty and/or ytick=\empty. Use xticklabels=\empty and yticklabels=\empty to not show the ticklabels, major thick length=0 and minor thick length=0 to not show the ticks.

You only want to see the minor grid? Or both? For the minor grid you have to set a minor tick num. In the following example I use both grids with minor thick num=3 and I color the major grid red.

enter image description here

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

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.