Skip to content

Instantly share code, notes, and snippets.

@vincentchu
Created May 18, 2011 02:39
Show Gist options
  • Save vincentchu/977892 to your computer and use it in GitHub Desktop.
Save vincentchu/977892 to your computer and use it in GitHub Desktop.
histogram.rb
#!/usr/bin/ruby
binwidth = ARGV[0].to_f
filename = ARGV[1]
rand_file_name = "rand_file-#{(rand*1000000).to_i}.data"
nums = File.open(filename, "r") {|f| f.readlines}.collect {|n| n.to_f }
binned_data = {}
nums.each do |n|
bin = binwidth * (n/binwidth).to_i + binwidth/2.0
binned_data[bin] ||= 0
binned_data[bin] += 1
end
ff = File.open(rand_file_name, "w")
binned_data.keys.sort.each {|k| ff.puts "#{k}, #{binned_data[k]}"}
ff.close
gnuplot_script = "plot '#{rand_file_name}' u 1:2 w boxes;"
`gnuplot -p -e "#{gnuplot_script}"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment