Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: building a plot of function

What's the easiest way to build a plot of a function under Ruby? Any suggestions as to the special graphical library?

update: under windows only :-(

update 2: found the following gem as a best solution so far https://github.com/clbustos/rubyvis

like image 777
gmile Avatar asked Oct 13 '09 17:10

gmile


3 Answers

Is gnuplot a possible option?:

require 'gnuplot.rb'
Gnuplot.open { |gp|
    Gnuplot::Plot.new( gp ) { |plot|
        plot.output "testgnu.pdf"
        plot.terminal "pdf colour size 27cm,19cm"

        plot.xrange "[-10:10]"
        plot.title  "Sin Wave Example"
        plot.ylabel "x"
        plot.xlabel "sin(x)"

        plot.data << Gnuplot::DataSet.new( "sin(x)" ) { |ds|
            ds.with = "lines"
            ds.linewidth = 4
        }
        plot.data << Gnuplot::DataSet.new( "cos(x)" ) { |ds|
            ds.with = "impulses"
            ds.linewidth = 4
        }
    }
}
like image 110
Brent.Longborough Avatar answered Nov 20 '22 02:11

Brent.Longborough


In case anyone else stumbles over this, I was able to use gnuplot using the following code:

require 'rubygems'
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
end

Requiring rubygems and using the correct gem name for gnuplot was the key for me.

like image 4
Thorsten Krüger Avatar answered Nov 20 '22 02:11

Thorsten Krüger


This is my go-to graphing library: SVG::Graph

like image 1
Matt Grande Avatar answered Nov 20 '22 01:11

Matt Grande