如何在gnuplot中分组箱线图的异常值

3
我有一组大量的数据点。我尝试用箱线图绘制它们,但其中一些异常值具有相同的值,并在一条线上表示。我找到了一个链接How to set the horizontal distance between outliers in gnuplot boxplot,但并没有太大帮助,因为显然不可能实现。

是否有可能将异常值分组,仅打印一个点,然后在其旁边打印一个数字括号以指示有多少个点?我认为这将使图表更易读。

作为参考,对于一个x值,我有三个箱线图,在一个图中重复六次。我正在使用gnuplot 5,并已经调整了点大小,但不能再缩小距离。 希望你能帮忙!

编辑:

set terminal pdf
set output 'dat.pdf'
file0 = 'dat1.dat'
file1 = 'dat2.dat'
file2 = 'dat3.dat'
set pointsize 0.2
set notitle
set xlabel 'X'
set ylabel 'Y'
header = system('head -1 '.file0);
N = words(header)

set xtics ('' 1)
set for [i=1:N] xtics add (word(header, i) i)

set style data boxplot
plot file0 using (1-0.25):1:(0.2) with boxplot lw 2 lc rgb '#8B0000' fs pattern 16 title 'A'
plot file1 using (1):1:(0.2) with boxplot lw 2 lc rgb '#00008B' fs pattern 4 title 'B'
plot file2 using (1+0.25):1:(0.2) with boxplot lw 2 lc rgb '#006400' fs pattern 5 title 'C'
for [i=2:N] plot file0 using (i-0.25):i:(0.2) with boxplot lw 2 lc rgb '#8B0000' fs pattern 16 notitle
for [i=2:N] plot file1 using (i):i:(0.2) with boxplot lw 2 lc rgb '#00008B' fs pattern 4 notitle
for [i=2:N] plot file2 using (i+0.25):i:(0.2) with boxplot lw 2 lc rgb '#006400' fs pattern 5 notitle

这段代码已经存在,如何最好地实现它?
1个回答

2

目前还没有自动完成此操作的选项。手动在gnuplot中完成所需步骤如下:

(在以下步骤中,我假设数据文件data.dat只有一列.)

  1. Analyze your data with stats to determine the boundaries for the outliers:

    stats 'data.dat' using 1
    range = 1.5 # (this is the default value of the `set style boxplot range` value)
    lower_limit = STATS_lo_quartile - range*(STATS_up_quartile - STATS_lo_quartile)
    upper_limit = STATS_up_quartile + range*(STATS_up_quartile - STATS_lo_quartile)
    
  2. Count only the outliers and write them to a temporary file

    set table 'tmp.dat'
    plot 'data.dat' using 1:($1 > upper_limit || $1 < lower_limit ? 1 : 0) smooth frequency
    unset table
    
  3. Plot the boxplot without the outliers, and the outliers with the labels plotting style:

    set style boxplot nooutliers
    plot 'data.dat' using (1):1 with boxplot,\
         'tmp.dat' using (1):($2 > 0 ? $1 : 1/0):(sprintf('(%d)', int($2))) with labels offset 1,0 left point pt 7
    

需要为每个箱线图执行此操作。

免责声明:这个步骤应该基本可行,但是由于没有示例数据,我无法测试它。


谢谢!这个解决方案真的很好。唯一的问题是,它会打印出每一个点,即使在使用boxplot时原本没有点。你能否也看一下我原帖中的编辑?我发布了我现在正在使用的内容,我必须说我显然不如你擅长Gnuplot。;) 是否还可以更改点的大小和数字的大小?PS:在您的第二个代码片段中需要使用upper_limit和lower_limit。;) - Patrick
绘制标签时,您必须检查计数是否> 0,我编辑了答案。 您可以像通常一样使用例如“ps 2”更改点大小,并且在使用“带标签的绘图”时还可以使用“字体”选项,例如“plot ... with labels font',12'”。 - Christoph
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Patrick
我可以重现这个错误,当在脚本开头设置cairolatex终端时。你必须在实际绘图之前直接设置cairolatex终端和输出文件,对于pdfcairo则无所谓。不知道为什么会发生这种情况。 - Christoph

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接