1

是否有一个通用的命令行 Unix 实用程序允许您使用文件中的一列浮点数创建一个简单的直方图?

4

3 回答 3

1

I can't think of any standard unix/gnu command line utility specifically for this. You can make a one-line script, like this:

awk '{nums[n++]=$1;if ($1 > max) max=$1} END {for (i=0; i<n; i++){ printf "%15.5f ",nums[i]; y=(nums[i] * 50)/max + 0.5; for(j=0; j<y; j++) printf "X";printf "\n";} }' < fileOfNumbers

here is fileOfNumbers

0
0.3802513823806563
0.6970557413810001
0.8987761906537762
0.9550334815954414
0.8619219017964542
0.6420060782538214
0.339191808764492
0.009613420853058656
-0.2895238011697386
-0.5108307062853145
-0.6251519043638415
-0.6258919548129064
-0.5285343220241233
-0.3656723046807657
-0.17881005669083072
-0.008834451885644044
0.11272730604816542
0.16980020199496673
0.16398989653539123
0.11263040266652444
0.04327594873440613
-0.013960516502076581
-0.034374900159696395
-0.005077671284778755

and here is output Histogram of above floats

0.00000 X
0.38025 XXXXXXXXXXXXXXXXXXXXX
0.69706 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0.89878 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0.95503 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0.86192 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0.64201 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0.33919 XXXXXXXXXXXXXXXXXXX
0.00961 XX
-0.28952 
-0.51083 
-0.62515 
-0.62589 
-0.52853 
-0.36567 
-0.17881 
-0.00883 X
0.11273 XXXXXXX
0.16980 XXXXXXXXXX
0.16399 XXXXXXXXXX
0.11263 XXXXXXX
0.04328 XXX
-0.01396 
-0.03437 
-0.00508 X
于 2013-09-26T14:06:39.247 回答
0

这是一个 Python one liner(在 Python 2.7.5 和 3.3.2 上测试)。它不太适合浮点数(对不起,OP),并且不会绘制图形或将数字分组到 bin 中。但它会计算不是数字的东西。也许对这种情况并不完全有帮助,但我认为多样性是某种东西的调味品。

python -c'import collections as c, sys, pprint as p; p.pprint(c.Counter(map(int, sys.stdin.read().split())))' < fileOfNumber
于 2013-10-27T22:32:22.080 回答
0

这是一些 python 将浮点输入文件绘制为直方图和曲线

#  usage
#
#  python plot_file_of_floats.py  < /tmp/my_file_of_floats


import matplotlib.pyplot as plt
import sys

floats = map(float, sys.stdin.read().split()) # read input file from command line


plt.plot(floats, 'ro') # show floats as curve
plt.figure()           # permit another plot to get rendered

plt.hist(floats)  #  show histogram of floats
plt.show()

浮点数的示例输入文件 /tmp/my_file_of_floats

0
0.3802513823806563
0.6970557413810001
0.8987761906537762
0.9550334815954414
0.8619219017964542
0.6420060782538214
0.339191808764492
0.009613420853058656
-0.2895238011697386
-0.5108307062853145
-0.6251519043638415
-0.6258919548129064
-0.5285343220241233
-0.3656723046807657
-0.17881005669083072
-0.008834451885644044
0.11272730604816542
0.16980020199496673
0.16398989653539123
0.11263040266652444
0.04327594873440613
-0.013960516502076581
-0.034374900159696395
-0.005077671284778755

这是使用上述浮点文件执行上述代码的方式

python plot_file_of_floats.py  < /tmp/my_file_of_floats

这是直方图

在此处输入图像描述

上面的python也显示了这条相同的浮动曲线

在此处输入图像描述

于 2018-04-14T20:53:03.097 回答