情報交換概論 第 6 回 (画像作成ソフトウェア実習) 「Gnuplot を Ruby で使用するには」 講義ノート目次

ruby+gnuplot が使える。

#!/usr/koeki/bin/ruby
#coding: euc-jp

require 'gnuplot'

 Gnuplot.open do |gp|
   Gnuplot::Plot.new( gp ) do |plot|
  
     plot.title  "Array Plot Example"
     plot.ylabel "x"
     plot.xlabel "x^2"
    
     x = (0..50).collect { |v| v.to_f }
     y = x.collect { |v| v ** 2 }

     plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
       ds.with = "linespoints"
       ds.notitle
     end
   end
 end
#!/usr/koeki/bin/ruby
# -*- coding: euc-jp -*-

require 'gnuplot'

Gnuplot.open do |gp|
  Gnuplot::Plot.new( gp ) do |plot|    
    plot.xrange "[-10:10]"
    plot.title  "Sin Wave Example"
    plot.ylabel "x"
    plot.xlabel "sin(x)"
        plot.data << Gnuplot::DataSet.new( "sin(x)" ) do |ds|
      ds.with = "lines"
      ds.linewidth = 4
    end    
  end  
  #sleep 10
end
#!/usr/koeki/bin/ruby
# -*- coding: euc-jp -*-

require 'gnuplot'


Gnuplot.open do |gp|
   File.open( "gnuplot.dat", "w") do |gp|
   Gnuplot::Plot.new( gp ) do |plot|
  
     plot.xrange "[-10:10]"
     plot.title  "Sin Wave Example"
     plot.ylabel "x"
     plot.xlabel "sin(x)"
    
     x = (0..50).collect { |v| v.to_f }
     y = x.collect { |v| v ** 2 }

     plot.data = [
       Gnuplot::DataSet.new( "sin(x)" ) { |ds|
 	ds.with = "lines"
 	ds.title = "String function"
 	ds.linewidth = 4
       },
    
       Gnuplot::DataSet.new( [x, y] ) { |ds|
 	ds.with = "linespoints"
 	ds.title = "Array data"
       }
     ]

   end
  
 end
#!/usr/koeki/bin/ruby
# -*- coding: euc-jp -*-

require 'gnuplot'


   Gnuplot::SPlot.new( gp ) do |plot|
  
     plot.xrange "[-10:10]"
     plot.yrange "[-10:10]"
     plot.title  "Example"
     plot.ylabel "y"
     plot.xlabel "x"
     plot.zlabel "z"

     x_dots = Range.new(-10, 10).to_a
     y_dots = Range.new(-10, 10).to_a

     data = Array.new
     z = Array.new
     x_dots.each_with_index do |x, i|
       z[i] = Array.new
       y_dots.each_with_index do |y, j|
         z[i][j] = x ** 2 + y ** 2        
       end
     end
     data = [x_dots, y_dots, z]
    
     plot.data << Gnuplot::DataSet.new( data ) do |ds|
       ds.with = "points"
     end    
   end
 end