如何解决Gnuplot:使用 gnuplot 创建直方图
我想使用 gnuplot 创建一个关于单列数据集中文本出现的直方图。我需要一些帮助。数据集示例如下:
UDP
TCP
TCP
UDP
ICMP
ICMP
ICMP
TCP
解决方法
有类似的问题,例如gnuplot automatic stack bar graph,
然而,还是有点不同。
以下示例创建了一些测试数据。
如果您已经知道关键字并希望拥有它们
按特定顺序,跳过创建唯一列表的步骤并自己定义 Uniques = '...'
。如果您的关键字包含空格,将项目括在双引号中可能会更有利。
- 创建一个独特的关键字列表。
- 通过(错误)使用 sum 函数定义查找函数(检查
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
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
结果:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。