class NumPlot::Plotter
The plotter class. To plot something, you need to create a Plotter
object, set parameters to the object, add data sets to the object, and call {#plot} as in the example below. @example
# An array of data to plot data = -2.0.step(2.0, 0.05).map{|x| [x, Math.sin(x)]} # Create a new Plotter object plotter = NumPlot::Plotter.new # Set parameters plotter.set("term", "png") plotter.output("sin.png") plotter.xrange -2.0 .. 2.0 # Set datasets plotter.datasets << NumPlot::Dataset.rows(data, title: "sin(x)", with: line) # Plot (output to sin.png) plotter.plot
Constants
- RANGE_OPTIONS
@!visibility private
Attributes
An array of all data sets. @deprecated @return [Array]
Public Class Methods
Create a new Plotter
object. You can specifiy the plot command (plot/splot) by plot_command parameter. @param plot_command “plot” or “splot”
# File lib/numplot.rb, line 254 def initialize(plot_command = "plot") @plot_command = plot_command @commands = [] @datasets = [] end
Public Instance Methods
Add a dataset to the plotter. @param dataset a dataset @return [self]
# File lib/numplot.rb, line 300 def add_dataset(dataset) @datasets << dataset self end
Disable an enhanced text mode of gnuplot.
# File lib/numplot.rb, line 356 def disable_enhanced_text_mode @enhanced = false set("termoption", "noenhanced") end
Enable an enhanced text mode of gnuplot.
# File lib/numplot.rb, line 350 def enable_enhanced_text_mode @enhanced = true set("termoption", "enhanced") end
Set output target. You can specify target file name. @param fname name of target file @return [void]
# File lib/numplot.rb, line 396 def output(fname) if fname.nil? set("output") else set("output", "\"#{fname}\"") end end
Plot given datasets with given parameters. @overload plot(opts={})
Open a gnuplot process and write commands and data to the process. You get the result of plotting on a window or an image file You can specify the following options: * :persist - Add -persist to gnuplot command. The default value is true. * :waiting - Wait a gnuplot process to be terminate. The default value is true. If this value is true and the diagram is drawed on a new window, the ruby program is stopped until the window is closed. On the other hand, if this value is false, the ruby program continue although the window remains opened. * :gnuplot_command - gnuplot command name. @param opts options @return [void]
@overload plot(io)
Output all commands and data to IO object. Normally, you specify {Process} object as io. For debug use, you can specify the StringIO object as io. @param io[#write, #puts] output target @return [void]
# File lib/numplot.rb, line 432 def plot(arg={}) if Hash === arg Process.open(arg.fetch(:persist, true), arg.fetch(:waiting, true), arg.fetch(:gnuplot_command, "gnuplot")) do |pr| plot_to_io(pr) end else plot_to_io(arg) end end
Gnuplot “set” command. This method is generic, but you can use some useful methods like {#xrange} and {#xlabel} for “set” command. @param key
the first argument for gnuplot "set" command like "term", "output".
@macro label
# File lib/numplot.rb, line 383 def x2label(*args); _label("x2label", *args); end
@macro range
# File lib/numplot.rb, line 332 def x2range(*args); _range("x2range", *args); end
@macro label
# File lib/numplot.rb, line 377 def xlabel(*args); _label("xlabel", *args); end
@macro range
# File lib/numplot.rb, line 326 def xrange(*args); _range("xrange", *args); end
@macro label
# File lib/numplot.rb, line 385 def y2label(*args); _label("y2label", *args); end
@macro range
# File lib/numplot.rb, line 334 def y2range(*args); _range("y2range", *args); end
@macro label
# File lib/numplot.rb, line 379 def ylabel(*args); _label("ylabel", *args); end
@macro range
# File lib/numplot.rb, line 328 def yrange(*args); _range("yrange", *args); end
@macro label
# File lib/numplot.rb, line 381 def zlabel(*args); _label("zlabel", *args); end
@macro range
# File lib/numplot.rb, line 330 def zrange(*args); _range("zrange", *args); end
Private Instance Methods
# File lib/numplot.rb, line 387 def _label(type, *args) set(type, LabelParser.parse(@enhanced, *args)) end
# File lib/numplot.rb, line 336 def _range(type, r, opts={}) gnuplot_options = "" RANGE_OPTIONS.each do |opt| gnuplot_options << opt.plot_arg(opts) end set(type, convert_range(r), gnuplot_options) end
Add a command string to the list of commands. The commands are executed when {#plot} is called. @param command a command to add @return [self]
# File lib/numplot.rb, line 269 def add_command(command) @commands << command self end
Output all commands and data to IO object.
# File lib/numplot.rb, line 446 def plot_to_io(pr) @commands.each{|cmd| pr.puts(cmd) } pr.write(@plot_command) pr.write(" ") pr.write(@datasets.map{|e| e.to_plot_arg(@enhanced) }.join(", ")) pr.write("\n") pr.write(@datasets.map{|e| e.to_plot_dataset }.compact.join("e\n")) end