class 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')
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
Could be an array of ranges or number of bins
Total height
Add a line showing normal distribution
Bottom margin
Left margin
Right margin
Top margin
Maximum value on x axis. Calculated automaticly from data if not set
Maximum value on y axis. Calculated automaticly from data if not set.
Minimum value on x axis. Calculated automaticly from data if not set
Minimum value on y axis. Set to 0 if not set
Histogram name
Total width
Public Class Methods
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
# 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
# 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
Returns SVG with scatterplot
# File lib/statsample/graph/histogram.rb, line 176 def to_svg rp=rubyvis_panel rp.render rp.to_svg end