class NumPlot::Dataset

The dataset class plotted by {Plotter}.

You can construct a dataset object using one of the following three methods:

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.

Constants

OPTIONS

@!visibility private

Public Class Methods

columns(data, opts={}) click to toggle source

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
histogram(data, opts={}) click to toggle source

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")

@param

# File lib/numplot.rb, line 599
def self.histogram(data, opts={})
  case opts.fetch(:dim, 1)
  when 1
    histo1d(data, opts)
  when 2
    raise NotImplementedError, "Histogram for 2D data is not implemented yet"
  else
    raise ArgumentError, "You can use only 1D or 2D histogram data"
  end
end
lines(data, opts={}) click to toggle source

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
new(data, opts={}) click to toggle source
# File lib/numplot.rb, line 496
def initialize(data, opts={})
  @data = data
  @opts = opts
end

Private Class Methods

bin1d(point, binsize, basepoint) click to toggle source

@!visibility private

# File lib/numplot.rb, line 631
def self.bin1d(point, binsize, basepoint)
  (point + binsize/2).div(binsize) * binsize
end
cumulative_1d(bins, style, numdata, binsize) click to toggle source

@!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
histo1d(data, opts) click to toggle source

@!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
histogram_plotdata_1d(bins, style, numdata, binsize) click to toggle source

@!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

to_plot_arg(in_enhanced_mode) click to toggle source

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
to_plot_dataset() click to toggle source

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