DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Use Gnuplot With Ruby And Ruby On Rails

03.31.2009
| 7585 views |
  • submit to reddit
        Gnuplot (http://gnuplot.sourceforge.net/) is a portable command-line driven interactive data and function plotting utility.

There is a Wrapper for Ruby:
* http://rubyforge.org/projects/rgplot/ 

Installation:
$> sudo gem install gnuplot

Use (example):
require 'gnuplot'
 
class DemosController < ApplicationController
 
  def index
 
    # tryout 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
 
 
    respond_to do |format|
      format.html # index.html.erb
    end
  end
 
end
    

Comments

Snippets Manager replied on Mon, 2010/03/08 - 6:26am

to be able to show the generated figure you need to save it, (plot.output filename) and then send_file... here a working example: http://gamba.me/2010/plot-graphs-and-figures-with-gnuplot-in-a-ruby-on-rails-app/