gnuplot - 使用 gnuplot 查找最接近零的值

标签 gnuplot

我想使用列的 gnuplot 自动找到最接近零的值。我尝试了这个,但它不起作用:

stats 'file.dat' u abs($1) 

然后我应该通过 STATS_min 得到最接近零的值

-0.018492   -0.00369832
-0.0106795  -0.00352183
-0.00612481 -0.0016998
-0.00221856 -0.00160066
0.00168769  -0.00148317
0.00559394  -0.00135358
0.00950019  -0.00119879
0.0134064   -0.00102774

你有什么想法吗? 谢谢

最佳答案

如果您执行以下操作并注意 abs($1) 周围的 ():

stats $Data u (abs($1)) nooutput
print STATS_min

您将得到:0.00168769,这对于您当前的数据来说是正确的。

但是,请小心,如果有一个值,例如-0.0001,这将返回+0.0001

因此,您必须使用“两”列进行统计,一列用于查找到零的最小距离,另一列用于返回带有原始符号的值。 因此,我故意在下面插入了一行 -0.0001 -0.99999

脚本:

### get nearest value to zero
reset session

$Data <<EOD
-0.018492   -0.00369832
-0.0106795  -0.00352183
-0.00612481 -0.0016998
-0.00221856 -0.00160066
-0.0001     -0.99999
0.00168769  -0.00148317
0.00559394  -0.00135358
0.00950019  -0.00119879
0.0134064   -0.00102774
EOD

stats $Data u 1:(abs($1)) nooutput

print STATS_pos_min_y
### end of script

结果:

-0.0001

添加:

这是一个没有统计数据的替代建议。如果您想标记提取的点,您可以在绘图命令期间提取坐标,即您只需浏览一次数据。

脚本:

### get nearest value to zero
reset session

$Data <<EOD
-0.018492   -0.00369832
-0.0106795  -0.00352183
-0.00612481 -0.0016998
-0.00221856 -0.00160066
-0.0001     -0.0015
0.00168769  -0.00148317
0.00559394  -0.00135358
0.00950019  -0.00119879
0.0134064   -0.00102774
EOD

set key noautotitle
set yzeroaxis

plot x0=y0=NaN $Data u (x0!=x0?x0=$1:abs($1)<abs(x0)?(y0=$2,x0=$1):$1):2 w p pt 7 lc "red", \
     '+' u (x0):(y0):(sprintf("(%g,%g)",x0,y0)) every ::::0 w labels offset 0,-1 left
### end of script

结果:

enter image description here

关于gnuplot - 使用 gnuplot 查找最接近零的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76488253/

相关文章:

gnuplot - Gnuplot 多图模式下的屏幕尺寸

Gnuplot - 使用直方图样式时如何将 y 值放在条上方?

osx-lion - gnuplot x11 和 OSX 10.8.2

gnuplot - 如何在 gnuplot 中添加两个具有不同 x 基准的文件文件

gnuplot - 在 Gnuplot 中导入字体 : strange result

plot - 在 gnuplot 中使用多图时如何确保我的图共享同一 Axis ?

gnuplot:数据矩阵的 3D 图

plot - 将误差线设为转换后的值

python-2.7 - 我可以对自变量进行数学计算,以便在 Gnuplot 中的 x 轴上显示吗?

gnuplot - gnuplot 中用户定义的 y 轴缩放比例(相当于设置 logscale y)