简体   繁体   English

计算 gnuplot 中的负值

[英]Counting negative values in gnuplot

I have a list a of data points read from file in gnuplot.我有一个从 gnuplot 文件中读取的数据点列表。 Is there any way to count the number of negative values in the list, and store the result as a variable?有没有办法计算列表中负值的数量,并将结果存储为变量?

I could not find anything upon a quick Google search.快速谷歌搜索后我找不到任何东西。

Thank you for your help.谢谢您的帮助。

There are at least two ways to extract the desired number, it depends how you want to use this number further.至少有两种方法可以提取所需的数字,这取决于你想如何进一步使用这个数字。

  1. count the negative values with stats (check help stats ) and a comparison.使用stats (检查help stats )和比较来计算负值。 $2<0 returns 1 or 0 . $2<0返回10 Hence, the variable STATS_sum will contain the number of negative values.因此,变量STATS_sum将包含负值的数量。

or alternatively或者

  1. count the negative values during plotting using the ternary operator (check help ternary ).使用三元运算符在绘图期间计算负值(检查help ternary )。 Plot the number into your plot using plotting style with labels . Plot 使用with labels的绘图样式将数字转换为您的 plot 。

Script:脚本:

### count negative values
reset session

# create some random test data
set table $Data
    plot '+' u 1:(rand(0)*10-1) w table
unset table

set key noautotitle
set xzeroaxis lt -1

stats $Data u ($2<0) nooutput
print sprintf("Negative values: %d", STATS_sum)

plot c=0 $Data u 1:($2<0?c=c+1:0,$2) w lp pt 7 lc "red" ti "Data", \
         '+' u (0):(10):(sprintf("Negative values: %d",c)) every ::::0 w labels offset 0,-1
### end of script

Result:结果:

Negative values: 6

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM