简体   繁体   中英

Counting negative values in gnuplot

I have a list a of data points read from file in 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. $2<0 returns 1 or 0 . Hence, the variable STATS_sum will contain the number of negative values.

or alternatively

  1. count the negative values during plotting using the ternary operator (check help ternary ). Plot the number into your plot using plotting style with labels .

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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