[Tex/LaTex] How to only draw grid lines for ticks with labels in pgfplots

gridspgfplots

I'm using the dynamic dict from mail-archive.com
It generates automatically the data for the x axis.

I want only every second (or every third, …) tick and grid line. I've already managed to omit the labels for the ticks, but the grid lines are still there :/

Here's the MWE:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{ifthen}
\usepackage{intcalc}

\newcount\kencounter
\global\kencounter=0
\global\def\xdata{}

\pgfplotsset{
    dynamic dict fewerticks/.style={
        x coord trafo/.code={%
            \pgfkeysifdefined{/ken/key ##1}{%
                 \pgfkeysgetvalue{/ken/key ##1}\pgfmathresult%
            }{%
                \ifthenelse{\equal{\intcalcMod{\the\kencounter}{#1}}{0}}{
                    \ifthenelse{\equal{\xdata}{}}
                    {\global\edef\xdata{##1}}
                    {\global\edef\xdata{\xdata,##1}}
                }{}
                \edef\pgfmathresult{\the\kencounter}%
                \global\pgfkeyslet{/ken/key ##1}\pgfmathresult
                \global\pgfkeyslet{/ken/key no \pgfmathresult}{##1}%
                \global\advance\kencounter by 1
            }%
        },
        x coord inv trafo/.code={%
            \pgfmathint{##1}%
            \pgfkeysifdefined{/ken/key no \pgfmathresult}{%
                    \pgfkeysgetvalue{/ken/key no \pgfmathresult}\pgfmathresult%
            }{%
                    \PackageError{pgfplots}{Inverse trafo for \pgfmathresult\space failed: no such key!}{}%
            }%
        },
        xticklabel={\ifthenelse{\equal{\intcalcMod{\ticknum}{#1}}{0}}{\tick}{}},
        scaled x ticks=false,
        plot coordinates/math parser=false
    },
}

\begin{document}

My tries:

\begin{tikzpicture}%
    \begin{axis}[width=7cm,dynamic dict fewerticks=2,grid=major, xtick=data,ylabel={\xdata}]%
        \addplot coordinates { (1M,2) (2M,4) (3M,6) };
    \end{axis}%
\end{tikzpicture}
%
\begin{tikzpicture}%
    \begin{axis}[width=7cm,dynamic dict fewerticks=2,grid=major, xtick={1M,3M},ylabel={\xdata}]%
        \addplot coordinates { (1M,2) (2M,4) (3M,6) };
    \end{axis}%
\end{tikzpicture}

\vspace{1cm}How it should look like (ignore the missing M):\vspace{1cm}

\begin{tikzpicture}%
    \begin{axis} [width=7cm,grid=major,xtick={1,3}]%
         \addplot coordinates { (1,2) (2,4) (3,6) };
    \end{axis}%
\end{tikzpicture}%

\end{document}

and why doesn't the upper right picture show the label "3M"?

enter image description here

Best Answer

This doesn't directly solve this question, but it solved my problem: I wanted a x axis with buffer sizes and units (B, KiB, MiB, GiB). For this the dynamic axis style worked, but there is a better solution:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{ifthen}
\pgfplotsset{
    byte x achsis log/.style={
                xticklabel={%
                    \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed,/pgf/number format/.cd,fixed,fixed zerofill}
                    \pgfmathparse{2^\tick}
                    \sizetobytes{\pgfmathresult}
                    },
                xticklabel style={rotate=90,anchor=east, font=\small},
    },
}


\def\less#1#2#3{%
    \begingroup%
        \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed,/pgf/number format/.cd,fixed,fixed zerofill,precision=0}%
        \def\arga{#1}\edef\earga{\arga}%
        \def\argb{#2}\edef\eargb{\argb}%
        \pgfmathparse{\arga < \argb}%
        \pgfmathprintnumberto[verbatim]{\pgfmathresult}{\lessresult}%
        \xdef#3{\lessresult}%
    \endgroup%
}

\def\sizetobytes#1{%
    \begingroup%
    \def\size{#1}%
    \less{\size}{1024}{\result}%
    \ifthenelse{\equal{\result}{1}}%
    {%
        \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed,%
        /pgf/number format/.cd,fixed,fixed zerofill,precision=0,set thousands separator={}}%
        \pgfmathprintnumber{\size}$\;$B%
    }%
    {%
        \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed,%
        /pgf/number format/.cd,fixed,fixed zerofill,precision=1,set thousands separator={}}%
        \less{\size}{1048576}{\result}%
        \ifthenelse{\equal{\result}{1}}{%
            \pgfmathparse{divide(\size,1024)}%
            \pgfmathprintnumber{\pgfmathresult}$\;$KiB%
        }%
        {%
            \less{\size}{1073741824}{\result}%
            \ifthenelse{\equal{\result}{1}}{%
                \pgfmathparse{divide(\size,1048576)}%
                \pgfmathprintnumber{\pgfmathresult}$\;$MiB%
            }%
            {%
                \pgfmathparse{divide(\size,1073741824)}%
                \pgfmathprintnumber{\pgfmathresult}$\;$GiB%
            }%
        }%
    }%
    \endgroup%
}%

\begin{document}

\begin{tikzpicture}%
    \begin{axis} [width=7cm,
                grid=major,
                xmode=log,
                log basis x=2,
                byte x achsis log,
                xminorticks=true,
                try min ticks log=13
                ]%
         \addplot coordinates { (1,2) (2048,4) (3000,6) (3000000,7)};
    \end{axis}%
\end{tikzpicture}%

\end{document}

result image