Parent

Included Modules

Class/Module Index [+]

Quicksearch

Statsample::Graph::Histogram

In statistics, a histogram is a graphical representation, showing a visual impression of the distribution of experimental data. It is an estimate of the probability distribution of a continuous variable and was first introduced by Karl Pearson [1]. A histogram consists of tabular frequencies, shown as adjacent rectangles, erected over discrete intervals (bins), with an area equal to the frequency of the observations in the interval. The height of a rectangle is also equal to the frequency density of the interval, i.e., the frequency divided by the width of the interval. The total area of the histogram is equal to the number of data.

Usage

Svg output

a=[1,2,3,4].to_scale
puts Statsample::Graph::Histogram.new(a).to_svg

Using ReportBuilder

a=[1,2,3,4].to_scale
rb=ReportBuilder.new
rb.add(Statsample::Graph::Histogram.new(a))
rb.save_html('histogram.html')

Attributes

name[RW]

Histogram name

width[RW]

Total width

height[RW]

Total height

margin_top[RW]

Top margin

margin_bottom[RW]

Bottom margin

margin_left[RW]

Left margin

margin_right[RW]

Right margin

hist[R]
bins[RW]

Could be an array of ranges or number of bins

minimum_x[RW]

Minimum value on x axis. Calculated automaticly from data if not set

maximum_x[RW]

Maximum value on x axis. Calculated automaticly from data if not set

minimum_y[RW]

Minimum value on y axis. Set to 0 if not set

maximum_y[RW]

Maximum value on y axis. Calculated automaticly from data if not set.

line_normal_distribution[RW]

Add a line showing normal distribution

Public Class Methods

new(data,opts=Hash.new) click to toggle source

data could be a vector or a histogram

# File lib/statsample/graph/histogram.rb, line 47
def initialize(data,opts=Hash.new)
  prov_name=(data.respond_to?(:name)) ? data.name : ""
  opts_default={
    :name=>_("Histograma (%s)") % prov_name,
    :width=>400,
    :height=>300,
    :margin_top=>10,
    :margin_bottom=>20,
    :margin_left=>30,
    :margin_right=>20,
    :minimum_x=>nil,
    :maximum_x=>nil,
    :minimum_y=>nil,
    :maximum_y=>nil,
    :bins=>nil,
    :line_normal_distribution=>false
  }
  @opts=opts_default.merge(opts)
  opts_default.keys.each {|k| send("#{k}=", @opts[k]) }
  @data=data
end

Public Instance Methods

report_building_text(generator) click to toggle source
# File lib/statsample/graph/histogram.rb, line 186
def report_building_text(generator)
  pre_vis
  #anchor=generator.toc_entry(_("Histogram %s") % [@name])
  step=  @hist.max_val > 40 ? ( @hist.max_val / 40).ceil : 1
    
  @hist.range.each_with_index do |r,i|
    next if i==@hist.bins
    generator.text(sprintf("%5.2f : %s", r, "*" * (@hist.bin[i] / step).floor ))
  end
end
rubyvis_normal_distribution(pan) click to toggle source
# File lib/statsample/graph/histogram.rb, line 80
def rubyvis_normal_distribution(pan)
  x_scale=@x_scale
  y_scale=@y_scale
  
  wob = @hist.get_range(0)[1] - @hist.get_range(0)[0]
  
  nob = ((@maximum_x-@minimum_x) / wob.to_f).floor
  sum=@hist.sum
  
  data=nob.times.map {|i|
    l=@minimum_x+i*wob
    r=@minimum_x+(i+1)*wob          
    middle=(l+r) / 2.0
    pi=Distribution::Normal.cdf((r-@mean) / @sd) - Distribution::Normal.cdf((l-@mean) / @sd)
    {:x=>middle, :y=>pi*sum}
  }
  pan.line do |l|
    l.data data
    l.interpolate "cardinal"
    l.stroke_style "black"
    l.bottom {|d| y_scale[d[:y]]}
    l.left {|d| x_scale[d[:x]]}
  end
  
end
to_svg() click to toggle source

Returns SVG with scatterplot

# File lib/statsample/graph/histogram.rb, line 176
def to_svg
  rp=rubyvis_panel
  rp.render
  rp.to_svg
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.