使用 pgfplots 包绘制箱线图

使用 pgfplots 包绘制箱线图

我使用 pgfplots 包制作了第一个盒子。我有几个问题:

如何用千位分隔符书写不带逗号的数字?

如何使盒子和 x 轴之间的距离更大?

\usemodule[tikz]
\usemodule[pgfplots]
\usepgfplotslibrary[statistics]
\pgfplotsset[width=12cm,compat=1.11]

\starttext

\starttikzpicture
\startaxis[y=1.5cm,
       axis y line=none,
       axis x line=bottom,
       enlarge x limits,
       thick
      ]

\addplot+[
          green,line width=0.5mm,
          boxplot prepared={
          median=1500,
          upper quartile=2500,
          lower quartile=1200,
          upper whisker=3000,
          lower whisker=1000
           },
          ] coordinates {};

\stopaxis
\stoptikzpicture
\stoptext

在此处输入图片描述

答案1

您的第一个问题可以通过本网站上的许多帖子来回答,例如,pgfplots 轴中的数字格式简单的解决方案是向轴标签添加数字格式的代码:

/pgf/number format/1000 sep={}

您的第二个问题可以通过设置来解决ymin=0(或者实际上,任何ymin值,0在我看来都是一个不错的选择)。

我注意到该\pgfplotsset线路给我出了错误,但我没有经验,context所以我不能确定是什么导致了错误。

该行上的错误是由于在选项周围使用了方括号而不是花括号。如果我更正错误,默认刻度会重叠,因此下面的代码中指定了合理数量的刻度。您应该根据需要进行更改。

最后,我更喜欢用逗号结束每行选项(甚至是最后一行)。这使得向选项添加新行变得更容易 - 我不必记得添加逗号,而且我的版本控制只会告诉我一行发生了变化,而不是两行。所以你会在这里看到这一点,但这纯粹是个人喜好 :-)

\usemodule[tikz]
\usemodule[pgfplots]
\usepgfplotslibrary[statistics]
\pgfplotsset{width=12cm, compat=1.11}

\starttext

\starttikzpicture
\startaxis[y=1.5cm,
       axis y line=none,
       axis x line=bottom,
       enlarge x limits,
       thick,
       x tick label style={
           /pgf/number format/1000 sep={}
       },
       xtick={800,1200,...,3200},
       ymin=0,
      ]

\addplot+[
          green,line width=0.5mm,
          boxplot prepared={
          median=1500,
          upper quartile=2500,
          lower quartile=1200,
          upper whisker=3000,
          lower whisker=1000,
           },
          ] coordinates {};

\stopaxis
\stoptikzpicture
\stoptext

在此处输入图片描述

相关内容