Ruby Gnuplot を触ってみた。

irb上でふと配列とかをplotしたくなることってあると思います。
と言う訳でRubyからGnuplotにplotしてくれるRuby Gnuplotを触ってみました。

以下スクリプト(ほとんどサンプルのまま)を.irbrcでrequireされるように書いとく。

def plot(ary)
  require "gnuplot"

  Gnuplot.open do |gp|
    Gnuplot::Plot.new( gp ) do |plot|

=begin
    plot.title  'title'
    plot.ylabel 'ylabel'
    plot.xlabel 'xlabel'
=end

      x = (0..ary.length-1).collect { |v| v.to_f }
      y = ary

      plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
        ds.with = "lines"
        ds.notitle
      end
    end
  end
end


で、

plot Array.new(101) { |x| (50-x)**2 }

とか書くと楽しくなる場合がある。



Arrayのnewメソッドにブロックを渡せるのは楽しいねー(結論)


追記:
ruby gnuplotubuntuのパッケージでインストールしました。