如何在 pgfplots 中更改准备好的箱线图的 x 坐标?

如何在 pgfplots 中更改准备好的箱线图的 x 坐标?

我想使用 来可视化数据pgfplot boxplots。我正在使用prepared箱线图。我唯一的问题是我不知道如何正确自定义箱线图的 x 轴值。x 轴值应该是128, 256, 512, 1024。我已经尝试了该draw position选项,但箱线图变得很拥挤:

挤压箱线图

如何改变 x 轴的值,同时保持箱线图彼此均匀分布? 在此处输入图片描述

最小工作示例:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{statistics}

\begin{document}
    
\begin{tikzpicture}
    \begin{axis}[
        boxplot/draw direction=y,
        ]
        \addplot+ [boxplot prepared={
            draw position   =128,
            lower whisker   =649, 
            lower quartile  =749,
            median          =829, 
            upper quartile  =990,
            upper whisker   =3171
        },] coordinates {};
        \addplot+ [boxplot prepared={
            draw position   =256,
            lower whisker   =653, 
            lower quartile  =761,
            median          =898, 
            upper quartile  =1031,
            upper whisker   =3104},
        ] coordinates {};
        \addplot+ [boxplot prepared={
            draw position   =512,
            lower whisker   =782, 
            lower quartile  =812,
            median          =835, 
            upper quartile  =1144,
            upper whisker   =2418},
        ] coordinates {};
            \addplot+ [boxplot prepared={
            draw position   =1024,
            lower whisker   =822, 
            lower quartile  =826,
            median          =952, 
            upper quartile  =1208,
            upper whisker   =4454},
        ] coordinates {};
    \end{axis}
\end{tikzpicture}

\end{document}

答案1

您可以使用以下选项更改 x 坐标:

xtick={1,2,3,4},
xticklabels={128, 256, 512, 1024}

结果:

生成的箱线图

相关内容