【问题标题】:How to group boxplot outliers in gnuplot如何在 gnuplot 中对箱线图异常值进行分组
【发布时间】:2015-06-05 20:03:04
【问题描述】:

我有大量数据点。我尝试用箱线图绘制它们,但一些异常值是完全相同的值,并且它们表示在彼此相邻的一条线上。我找到了How to set the horizontal distance between outliers in gnuplot boxplot,但这并没有太大帮助,因为这显然是不可能的。

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

关于信息,我有一个 x 值的三个箱线图,然后在一个图中乘以六个。我正在使用 gnuplot 5,并且已经使用了 pointsize,这不再减少距离。 希望能帮到你!

编辑:

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

使用此代码实现它的最佳方法是什么?

【问题讨论】:

    标签: gnuplot boxplot outliers


    【解决方案1】:

    没有选项可以自动完成。在 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. 绘制没有异常值的箱线图,以及使用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
      

    这需要为每一个箱线图完成。

    免责声明:这个程序基本上应该可以工作,但是没有示例数据我无法测试它。

    【讨论】:

    • 谢谢!这个解决方案真的很好。唯一的问题是它会打印出每个点,即使是那些原本没有点的点,如果你用箱线图打印的话。你也可以看看我原来帖子中的编辑吗?我发布了我现在正在使用的内容,我不得不说我在 Gnuplot 显然不适应你。 ;) 是否也可以更改点的大小和数字的大小? PS:它需要在你的第二个代码sn-p中是upper_limit和lower_limit。 ;)
    • 绘制标签时,您必须检查计数是否 > 0,我编辑了答案。您可以像往常一样更改点大小,例如ps 2 也可以在绘制with labels 时使用font 选项,如plot ... with labels font ',12'
    • 出于测试目的,我使用了 pdf 终端,但我想在最后使用 cairolatex。我意识到使用 cairolatex 根本不会打印点和标签。你知道绕过它的方法吗?
    • 在脚本开头设置cairolatex 终端时,我可以重现此错误。您只能在进行实际绘图之前直接设置cairolatex 终端和输出文件,因为pdfcairo 没关系。不知道为什么会这样。
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多