Graphics

The GSL library itself does not include any utilities to visualize computation results. Some examples found in the GSL manual use GNU graph to show the results: the data are stored in data files, and then displayed by using GNU graph. Ruby/GSL provides simple interfaces to GNU graph to plot vectors or histograms directly without storing them in data files. Although the methods described below do not cover all the functionalities of GNU graph, these are useful to check calculations and get some speculations on the data.

Plotting vectors



Drawing histogram


Plotting Functions


Other way

The code below uses GNUPLOT directly to plot vectors.

#!/usr/bin/env ruby
require("gsl")
x = Vector.linspace(0, 2*M_PI, 50)
y = Sf::sin(x)
IO.popen("gnuplot -persist", "w") do |io|
  io.print("plot '-'\n")
  x.each_index do |i|
    io.printf("%e %e\n", x[i], y[i])
  end
  io.print("e\n")
  io.flush
end

It is also possible to use the Ruby Gnuplot library.

require("gnuplot")
require("gsl")
require("gsl/gnuplot");

Gnuplot.open do |gp|
  Gnuplot::Plot.new( gp ) do |plot|

    plot.xrange "[0:10]"
    plot.yrange "[-1.5:1.5]"
    plot.title  "Sin Wave Example"
    plot.xlabel "x"
    plot.ylabel "sin(x)"
    plot.pointsize 3
    plot.grid

    x = GSL::Vector[0..10]
    y = GSL::Sf::sin(x)

    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

prev

Reference index top