J
Jason Lillywhite
I want to create a class that uses the RubyGnuplot library that can
create a plot using one simple command and only add options if so
desired. Below is what I have done so far and I was wondering if someone
could provide some opinion on my approach. I ran into a problem where I
allow the user to add 3 options but what I really want is access to all
the library's methods so colors and lineweights, etc can be changed if
so desired. Is there a better approach than using a hash input like I'm
doing? Thank you!
#For simple 2D line plots. File is called 'my_plotter.rb'
require 'gnuplot'
class Data2d
def self.plot(x, y, options={})
Gnuplot.open do |gp|
Gnuplot:lot.new( gp ) do |plot|
plot.title "#{options[:title]}"
plot.ylabel "#{options[:y_label]}"
plot.xlabel "#{options[:x_label]}"
plot.data << Gnuplot:ataSet.new( [x, y] ) do |ds|
ds.with = "lines"
end
end
end
end
end
#Now all users need to do is create the data,
require 'my_plotter'
x = [1,2,3,4,5]
y = [5,6,3,8,9]
#then run the command,
Data2d.plot(x,y)
#or, with options,
Data2d.plot(x,y,:x_label=>"Time",:y_label=>"Values", :title=>"My First
Plot")
create a plot using one simple command and only add options if so
desired. Below is what I have done so far and I was wondering if someone
could provide some opinion on my approach. I ran into a problem where I
allow the user to add 3 options but what I really want is access to all
the library's methods so colors and lineweights, etc can be changed if
so desired. Is there a better approach than using a hash input like I'm
doing? Thank you!
#For simple 2D line plots. File is called 'my_plotter.rb'
require 'gnuplot'
class Data2d
def self.plot(x, y, options={})
Gnuplot.open do |gp|
Gnuplot:lot.new( gp ) do |plot|
plot.title "#{options[:title]}"
plot.ylabel "#{options[:y_label]}"
plot.xlabel "#{options[:x_label]}"
plot.data << Gnuplot:ataSet.new( [x, y] ) do |ds|
ds.with = "lines"
end
end
end
end
end
#Now all users need to do is create the data,
require 'my_plotter'
x = [1,2,3,4,5]
y = [5,6,3,8,9]
#then run the command,
Data2d.plot(x,y)
#or, with options,
Data2d.plot(x,y,:x_label=>"Time",:y_label=>"Values", :title=>"My First
Plot")