pgfplot 对数轴每十年有多个刻度标签(1,2,5,10)

pgfplot 对数轴每十年有多个刻度标签(1,2,5,10)

我希望我的对数轴有合理数量的刻度标签,这些刻度标签与“好”的数字相对应。例如,1-2-5-10 几乎是一个等比数列,因此它们在对数轴上的间距大致相等。

我尝试了这种风格log ticks with fixed point,但它做了像下面这样的愚蠢的事情:

“固定点对数刻度”的默认行为

第一个轴没有足够的标签,标签之间留有大量空白。第二个轴选择将主刻度放在十的幂的中间 - 例如 (.001, .01, .1) 的平方根,并且没有明显的对数刻度间距,因此读者不太可能注意到它是一个对数刻度。

这两个看起来都不好。我也试过了

    extra x ticks={0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100},
    extra x tick label={ \pgfmathfloatparsenumber{\tick} \pgfmathfloatexp{\pgfmathresult} \pgfmathprintnumber{\pgfmathresult} },
    xticklabels={},

这接近

用“额外 x 个刻度”控制刻度标签

但仍然有几个问题。标签没有正确居中在刻度上,右侧图表仍然将刻度放在 sqrt(0.001, 0.01, 0.1) 处。额外的刻度看起来像主刻度,这实际上不是问题,但也不理想。

我确实意识到这个数据范围并不真正需要对数刻度,它位于中间区域,可以任意取值。我使用对数是为了与其他跨越三十年的数据保持一致。

我怎样才能获得正确的小勾和标签?


完成 MWE

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{statistics}

\pgfplotsset{compat=1.9}

\begin{document}

\pgfplotsset{
    S/.style={
        xmode=log,
        log ticks with fixed point,
        ytick={1,2}
    },
}

\resizebox{.9 \columnwidth}{!}{
   \begin{tikzpicture}
       \begin{groupplot}[S,
         group style={columns=2},
         boxplot/draw direction=x,
       ]
       \nextgroupplot[title={90th Percentile Error}]
\addplot+[boxplot] table[header=false,row sep=\\,y index=0] {
 0.101411 \\ 0.072893 \\ 0.093314 \\ 0.075811 \\ 0.080916 \\ 0.063481 \\ 0.092617 \\ 0.113238 \\ 0.093469 \\ 0.073329 \\ 0.081212 \\ 0.092889 \\ 0.088329 \\ 0.096120 \\
};
\addplot+[boxplot] table[header=false,row sep=\\,y index=0] {
 1.032139 \\ 0.791053 \\ 0.659514 \\ 0.700048 \\ 0.523935 \\ 0.779241 \\ 0.891047 \\ 0.626462 \\ 1.141396 \\ 0.959789 \\ 1.110311 \\ 1.205517 \\ 1.058122 \\ 1.278061 \\ 0.801587 \\
};

       \nextgroupplot[title={Mean Absolute Error}]
\addplot+[boxplot] table[header=false,row sep=\\,y index=0] {
 0.051834 \\ 0.037930 \\ 0.057205 \\ 0.037753 \\ 0.033726 \\ 0.030318 \\ 0.046359 \\ 0.056751 \\ 0.036272 \\ 0.038457 \\ 0.039852 \\ 0.040726 \\ 0.047210 \\ 0.047129 \\ 0.042315 \\ 0.052287 \\ 0.048230 \\ 0.044014 \\ 0.039751 \\ 0.059421 \\ 0.043587 \\ 0.035238 \\ 0.039131 \\ 0.034533 \\ 0.045348 \\ 0.037391 \\ 0.048204 \\
};
\addplot+[boxplot] table[header=false,row sep=\\,y index=0] {
 0.334964 \\ 0.316966 \\ 0.269175 \\ 0.251022 \\ 0.225320 \\ 0.245171 \\ 0.322015 \\ 0.281091 \\ 0.367715 \\ 0.336092 \\ 0.375951 \\ 0.467985 \\ 0.407952 \\ 0.289091 \\
};
       \end{groupplot}
   \end{tikzpicture}
}

\end{document}

答案1

最终结果如下:

在此处输入图片描述

消除这些 sqrt 值的关键是不使用任何默认刻度。然后,您将失去默认的次要刻度,因此您必须手动定义次要刻度:

logaxisx/.style={
    xmode=log,
    xtick={0.02,0.05,0.2,0.5,2,5,20,50},
    extra x ticks={0.01,0.1,1,10,100},
    minor xtick={0.03,0.04,0.06,0.07,0.08,0.09,0.3,0.4,0.6,0.7,0.8,0.9,3,4,6,7,8,9,30,40,60,70,80,90},
    %xticklabel={\pgfmathfloatparsenumber{\tick}\pgfmathfloatexp{\pgfmathresult}\pgfmathprintnumber{\pgfmathresult}},
    log ticks with fixed point,
    extra x tick style={/pgfplots/major tick length=.2cm,/pgfplots/tick style={line width=1.5pt}},
}

对齐不良是由于xticklabel当我从以下位置复制值时,由于值画廊,这些问题也已得到修复。

如果需要的话,最后一个extra x tick style可以将十年与其他标记的刻度区分开来。

在我看来,相当多的额外代码应该是的默认结果log ticks with fixed point

更新:我log ticks with fixed point再次尝试,它确实设置了正确的数字格式,因此xticklabel不需要复杂的操作。感谢 Christian。但仍然需要手动设置xtickextra x ticksminor xtick列表。

答案2

这其实更像是一条评论,而不是答案。但由于它太长,并且包含图片,所以我想将其发布在这里。

简短的评论是:我无法重现该问题。我尝试了

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.9}

\begin{document}

\pgfplotsset{
    S/.style={
        scale only axis,
        height=1cm,
        width=8cm, 
        axis x line=bottom,
        axis y line=none,
        ytick=\empty,
        xmin=0.1,xmax=10,
        enlarge x limits,
        ymin=-1,ymax=1,
        xtick={0.1,0.2,0.5,1,2,5,10},
    },
}

\begin{tikzpicture}
    \begin{semilogxaxis}[
        S,
        log ticks with fixed point,
    ]
    \end{semilogxaxis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{semilogxaxis}[S]
    \end{semilogxaxis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

据我所见,这些描述都是正确的,因为它“几乎”是一个几何级数。

extra x ticks暂时退出了游戏,因为它似乎是解决方案的一部分,而不是问题的一部分。

我是否可以以请求提供一个完整的最小工作示例来结束我的“回答评论”,以便我们能够看到您要实现的目标的全貌以及如何重现问题?无论如何,这样的最小工作示例都是很好的做法。

相关内容