如何仅为 pgfplots 中带有标签的刻度绘制网格线?

如何仅为 pgfplots 中带有标签的刻度绘制网格线?

我正在使用来自的动态字典邮件存档网站 它自动生成 x 轴的数据。

我只需要每隔一秒(或每三秒……)刻度和网格线。我已经设法省略了刻度的标签,但网格线仍然存在 :/

这是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}

为什么右上角的图片没有“3M”标签?

在此处输入图片描述

答案1

这并不能直接解决这个问题,但它解决了我的问题:我想要具有缓冲区大小和单位(B、KiB、MiB、GiB)的 ax 轴。为此,动态轴样式有效,但有一个更好的解决方案:

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

结果图像

相关内容