如何在gnuplot中对boxplot异常值进行分组

时间:2015-04-01 08:39:39

标签: gnuplot boxplot outliers

我有一大堆数据点。我尝试用箱线图绘制它们,但是一些异常值是完全相同的值,它们在彼此相邻的一条线上表示。我找到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 个答案:

答案 0 :(得分:0)

没有选项可以自动完成此操作。在gnuplot中手动执行此操作所需的步骤是:

(在下面我假设,数据文件data.dat只有一列。)

  1. 使用stats分析您的数据,以确定异常值的边界:

    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. 仅计算异常值并将其写入临时文件

    set table 'tmp.dat'
    plot 'data.dat' using 1:($1 > upper_limit || $1 < lower_limit ? 1 : 0) smooth frequency
    unset table
    
  3. 绘制没有异常值的boxplot,以及labels绘图样式的异常值:

    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
    
  4. 这需要针对每个箱形图进行。

    免责声明:此程序基本上可行,但没有示例数据,我无法对其进行测试。

相关问题