Skip to content

Instantly share code, notes, and snippets.

@vincentchu
Created December 19, 2011 23:06
Show Gist options
  • Save vincentchu/1499331 to your computer and use it in GitHub Desktop.
Save vincentchu/1499331 to your computer and use it in GitHub Desktop.
#!/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}"`
# File.delete(rand_file_name)
# puts "histogramming #{filename} with binwidth = #{binwidth}"
#
# gnuplot_script = <<GNUPLOT
# binwidth=[[BINWIDTH]]; bin(x,width)=width*floor(x/width) + binwidth/2.0; plot '[[FILENAME]]' using (bin(\\$1,binwidth)):(1.0) smooth freq with boxes;
# GNUPLOT
#
# gnuplot_script = <<GNUPLOT
# binwidth=[[BINWIDTH]]; bin(x,width)=width*floor(x/width) + binwidth/2.0; plot '[[FILENAME]]' using (bin(\\$1,binwidth)):(1.0) with boxes;
# GNUPLOT
#
#
# gnuplot_script.gsub!("[[BINWIDTH]]", binwidth)
# gnuplot_script.gsub!("[[FILENAME]]", filename)
#
# puts "#{gnuplot_script}"
#
# `gnuplot -p -e "#{gnuplot_script}"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment