views:

629

answers:

5

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 :-(

A: 

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

Matt Grande
This one's the only worked for me. But - can I produce continuous function graph instead of just dotted?
gmile
A: 

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
        }
    }
}
Brent.Longborough
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
Sorry if this wasn't obvious, but have you got gnuplot itself installed? The Ruby bit is only the bindings, AFAIK
Brent.Longborough
A: 

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

Peter
tioga is for POSIX OS Family only :-(
gmile
ah, got that after your windows update.
Peter
A: 

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();
avguchenko
A: 

I have problem installing TIOGA. Prblem is zlib. It could not find zlib.h any help please.

Yebio