Gnuplot:使用 gnuplot 创建直方图

Gnuplot: Creating a histogram with gnuplot

我想使用 gnuplot 创建一个关于单列数据集中文本出现的直方图。我需要一些帮助。数据集示例如下:

UDP
TCP
TCP
UDP
ICMP
ICMP
ICMP
TCP

还有类似的问题,例如, 然而,还是有点不同。 以下示例创建了一些测试数据。 如果您已经知道关键字并希望拥有它们 按照一定的顺序,跳过创建唯一列表的步骤,自己定义Uniques = '...'。如果您有包含空格的关键字,将项目括在双引号中可能是有利的。

  • 创建一个独特的关键字列表。
  • 通过(误)使用求和函数定义查找函数(检查 help sum
  • 通过将查找索引作为 x
  • 使用绘图选项平滑(选中 help smooth frequency

代码:

### histogram: occurrences of keywords
reset session

# create some random test data
myKeywords = 'UDP TCP ICMP ABC WWW NET COM FTP HTTP HTTPS'
set print $Data
    do for [i=1:3000] {
        print word(myKeywords,int(rand(0)*10)+1)
    }
set print

# create a unique list of strings from a column
addToList(list,col) = list.( strstrt(list,'"'.strcol(col).'"') > 0 ? '' : ' "'.strcol(col).'"')
set table $Dummy
    plot Uniques='' $Data u (Uniques=addToList(Uniques,1),'') w table
unset table

N = words(Uniques)
Lookup(s) = (sum [_i=1:N] (s eq word(Uniques,_i)) ? _idx=_i : 0), _idx)

set xrange [1:N]
set xtics out
set ylabel "Counts"
set grid x,y
set offsets 0.5,0.5,0.5,0
set boxwidth 0.8

set style fill transparent solid 0.5 border
set key noautotitle

plot $Data u (Lookup(strcol(1))):(1):xtic(1) smooth freq w boxes
### end of code

结果: