class NumPlot::Dataset
The dataset class plotted by {Plotter}.
You can construct a dataset object using one of the following three methods:
-
{.columns}: construct a dataset from columns
-
{.rows}: construct a dataset from rows
-
{.histogram}: construct a histogram dataset
You need to choose from {.columns} and {.rows} according to the arrangement of your data.
The dataset object is registered to {Plotter} object using {Plotter#add_dataset}.
# Parameters for a dataset
When you call {.columns}, {.rows}, or {.histogram}, you can give some parameters to determine the style of plotting. The parameters are as follows. If you know the details of these parameters, please read the gnuplot documents.
-
title: “TITLE” or notitle: true
-
with: “points”, “dots”, etc.
-
axis:
-
linestyle: a style number (ls: is also available)
-
linetype: a line type number
-
linewidth: a line width multiplier (default is 1.0), (lw: is also available)
-
linecolor: a color number or an {RGB} object. (lc: is also available)
-
pointtype: a point type number (pt: is also available)
-
pointsize: a point size multiplier (default is 1.0), (ls: is also available)
-
fill: a fill style (fs: is also available)
-
nohidden3d: true if you need to activate this option
-
nocontours: true if you need to activate this option
-
nosurface: true if you need to activate this option
Constants
- OPTIONS
@!visibility private
Public Class Methods
Construct a dataset object form columns.
If your data are xs = [x0, x1, x2, … ] and ys = [y0, y1, y2, …], you should use this method as:
Dataset.columns([xs, ys])
If your data is transposed, you need to use {.columns} instead of this method.
You can give some parameters as a hash to set a plotting style. Please see {Dataset}. @param
# File lib/numplot.rb, line 516
def self.columns(data, opts={})
new(data.map(&:to_a).transpose, opts)
end
Construct a histogram dataset object.
You need to give some parameters to construct a histogram.
-
dim: the dimension of your data, now only 1-dim data is supported (default is 1)
-
binsize: the bin size (required, Numeric)
-
base: the coordinate of a center of a bin (default is 0.0). If binsize = 1.0 and base = 0.0, the bins are …, [-1.5, -0.5), [-0.5, 0.5), [0.5, 1.5), … . If binsize = 1.0 and base = 0.5, the bins are …, [-1.0, 0.0), [0.0, 1.0), … .
-
histogram_style: You can choose one of the following normalization scheme:
* :count not normalized * :ratio normailized to the number of data points * :density normalized to (the number of data points)*(binsize). You choose this option if you want to show the probability density function from the data.
-
cumulative: construct a cumulative histogram if true. If the cumulative option is true, the meaning of histogram_style option is changed.
* :count not normalized * :ratio, :density normalized by the number of data, this means that the maximum value of a cumulative histogram is normalized to 1.
You can also give some other parameters to determine a plotting style. See {Dataset} for details.
@example
require 'randomext' # install randomext gem # 1000 random samples from the standard normal distribution. rng = Random.new data = Array.new(1000){ rng.standard_normal } datasets = Dataset.histo(data, binsize: 0.1, histogram_style: :density, title: "Random samples from the normal distribution")
Construct a dataset object from a sequence of sequences of rows
If your data is like
[[[x00, y00] [x01, y01], ... ], [[x10, y10], [x11, y11], ...], ..]
you should use this method.
You can give some parameters as a hash to set a plotting style. Please see {Dataset} for details. @param
# File lib/numplot.rb, line 551
def self.lines(data, opts={})
data = data.map{|lines|
lines.to_a + [[]]
}.flatten(1)
p data
new(data, opts)
end
# File lib/numplot.rb, line 496 def initialize(data, opts={}) @data = data @opts = opts end
Construct a dataset object form rows.
If your data is [[x0, y0], [x1, y1], …], you should use this method as:
Dataset.rows(data)
If your data is transposed, you need to use {.rows} instead of this method.
You can give some parameters as a hash to set a plotting style. Please see {Dataset} for details. @param
# File lib/numplot.rb, line 535
def self.rows(data, opts={})
new(data.map(&:to_a), opts)
end
Private Class Methods
@!visibility private
# File lib/numplot.rb, line 631 def self.bin1d(point, binsize, basepoint) (point + binsize/2).div(binsize) * binsize end
@!visibility private
# File lib/numplot.rb, line 646 def self.cumulative_1d(bins, style, numdata, binsize) cumulative_data = [] bins.sort_by{|k, v| k }.inject(0.0){|sum, (k, v)| cumulative_data << [k, sum+v] sum + v } r = case style when :count then 1.0 when :ratio, :density then numdata end cumulative_data.map{|k, v| [k, v/r, binsize] } end
@!visibility private
# File lib/numplot.rb, line 611 def self.histo1d(data, opts) binsize = opts.fetch(:binsize).to_f basepoint = opts.fetch(:base, 0).to_f histo_style = opts.fetch(:histogram_style, :count) cumulative = opts.fetch(:cumulative, false) bins = Hash.new(0) data.each {|point| bins[bin1d(point, binsize, basepoint)] += 1 } if cumulative plotdata = cumulative_1d(bins, histo_style, data.size, binsize) else plotdata = histogram_plotdata_1d(bins, histo_style, data.size, binsize) end opts[:with] ||= "boxes" Dataset.rows(plotdata, opts) end
@!visibility private
# File lib/numplot.rb, line 636 def self.histogram_plotdata_1d(bins, style, numdata, binsize) r = case style when :count then 1.0 when :ratio then numdata.to_f when :density then numdata*binsize end bins.map{|k, v| [k, v/r, binsize]} end
Public Instance Methods
Return a stirng for gnuplot’s “plot” or “splot” like “‘-’ with dots” @return [String] @!visibility private
# File lib/numplot.rb, line 666 def to_plot_arg(in_enhanced_mode) @opts[:_in_enhanced_text_mode] = in_enhanced_mode ret = "'-'" OPTIONS.each do |opt| ret << opt.plot_arg(@opts) end ret end
Return a string for gnuplot’s data like “1 2 3n4 4 6ne” @!visibility private
# File lib/numplot.rb, line 678 def to_plot_dataset @data.map{|dataline| dataline.join(" ")}.join("\n") + "\ne" end