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

13.10.2009 17:27:16
6 ОТВЕТОВ
РЕШЕНИЕ

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
        }
    }
}
7
13.10.2009 18:02:37
gnuplot is very sweet, but somehow I cannot get the examples running. Just installed the gem. Running 1.8.6 on Windows. Any ideas/required plugins?
gmile 13.10.2009 19:10:39
Sorry if this wasn't obvious, but have you got gnuplot itself installed? The Ruby bit is only the bindings, AFAIK
Brent.Longborough 13.10.2009 23:29:06
In response to @Brent: here is the page to download gnuplot: gnuplot.info/download.html
Alexander Bird 4.12.2014 22:55:35

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

1
13.10.2009 17:39:02
This one's the only worked for me. But - can I produce continuous function graph instead of just dotted?
gmile 13.10.2009 19:34:52

I really like tioga. It can produce incredibly high quality, publication-ready graphs in latex.

0
13.10.2009 18:26:55
tioga is for POSIX OS Family only :-(
gmile 13.10.2009 19:35:27
Actually, Tioga works on windows now, although probably the requirements for a MikTeX installation is painful.
Vincent Fourmond 3.09.2015 20:54:22

use SVG::Graph::Line like this:

require 'SVG/Graph/Line'

  fields = %w(Jan Feb Mar);
  data_sales_02 = [12, 45, 21]
  data_sales_03 = [15, 30, 40]

  graph = SVG::Graph::Line.new({
          :height => 500,
          :width => 300,
    :fields => fields,
  })

  graph.add_data({
          :data => data_sales_02,
    :title => 'Sales 2002',
  })

  graph.add_data({
          :data => data_sales_03,
    :title => 'Sales 2003',
  })

  print "Content-type: image/svg+xml\r\n\r\n";
  print graph.burn();
0
15.10.2009 17:43:36

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.

4
26.04.2011 17:03:37

There's Microsoft Excel.

If so, the Ruby on Windows blog may be useful, as are questions tagged win32ole and ruby.

0
23.05.2017 10:32:55