LoginSignup
7

More than 5 years have passed since last update.

hystでコマンドラインでヒストグラムを書く

Last updated at Posted at 2016-06-21

一年で300回ぐらいコマンドラインでヒストグラムを書きたくなるのでhystというコマンドをGoで書きました。

処理内容は以下の記事を参考にしています。

Installation

OSXの場合はHomebrewでインストールできます。

brew install https://raw.githubusercontent.com/winebarrel/hyst/master/homebrew/hyst.rb

その他のOSはアーカイブを展開すれば実行できます。

使い方

数値以外

数値以外の場合は、それぞれの値をカウントして多い順に並べます。

$ ruby -e 'puts 1000.times.map { %w(foo bar zoo)[rand(3)] }' > rand.txt

$ head rand.txt
zoo
zoo
bar
foo
zoo
bar
zoo
zoo
bar
zoo

$ cat rand.txt | hyst
zoo  351  ##################################################
foo  341  ################################################
bar  308  ###########################################

数値

-wオプションで階級の幅を指定すると、各値を数値として処理します。
以下は階級の幅を50として正規分布のヒストグラムを描画した例です。

$ gem install distribution

$ ruby -rdistribution -e 'normal = Distribution::Normal.rng(1); puts 1_000.times.map {normal.call * 100}' > gauss.txt

$ head gauss.txt
213.57863028255827
-103.40921634097238
-11.296606231306994
162.5407288364103
110.5574029438751
24.64428067386242
30.02832601441755
100.63481149177946
-23.661175314447913
185.2196657484519

$ cat gauss.txt | hyst -w 50
-250    1
-200    2
-150    4
-100   12  ##
 -50   51  ##########
   0  247  ##################################################
  50  198  ########################################
 100  198  ########################################
 150  137  ###########################
 200   96  ###################
 250   36  #######
 300   15  ###
 350    2
 400    1

似たようなツール

aggでアクセスログを単位時間ごとに集計する - Qiita

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7