微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Gnuplot:使用 gnuplot 创建直方图

如何解决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

结果:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?