Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | gnuplot + gnuplot gem でグラフを操作

概要

gnuplot + gnuplot gem でグラフを操作

詳細

gnuplot + gnuplot gem でグラフを操作します。

設定

Windows + Cygwin環境

Cygwinのsetup.exeでgnuplotをインストール

gnuplot gemをインストール

$ gem install gnuplot
サンプルコード

y=x*2 のグラフを出力します

# encoding: utf-8
require "gnuplot"

gnuplot.open do |gp|
  gnuplot::Plot.new( gp ) do |plot|
    plot.title  'y = x*2'
    plot.ylabel 'x'
    plot.xlabel 'y'
    plot.set "size ratio 1"
    plot.terminal("png")
    plot.output("y_eq_x2.png")
    plot.set "linestyle 1 linecolor rgbcolor 'blue' linetype 1"
    x = (-100..100).collect {|v| v.to_f}
    y = x.map { |v|v*2 }

    plot.data << gnuplot::DataSet.new( [x, y] ) do |ds|
      ds.with = "lines ls 1"
      ds.linewidth = 4
      ds.notitle
    end
  end
end
出力