Class: UnicodePlot::Stemplot
- Inherits:
-
Object
- Object
- UnicodePlot::Stemplot
- Defined in:
- src/lib/unicode_plot/stemplot.rb
Overview
Description
Draw a stem-leaf plot of the given vector +vec+.
stemplot(vec, **kwargs)
Draw a back-to-back stem-leaf plot of the given vectors +vec1+ and +vec2+.
stemplot(vec1, vec2, **kwargs)
The vectors can be any object that converts to an Array, e.g. an Array, Range, etc.
If all elements of the vector are Numeric, the stem-leaf plot is classified as a
NumericStemplot, otherwise it is classified as a StringStemplot. Back-to-back
stem-leaf plots must be the same type, i.e. String and Numeric stem-leaf plots cannot
be mixed in a back-to-back plot.
Usage
stemplot(vec, [vec2], scale:, divider:, padchar:, trim: )
Arguments
- +vec+: Vector for which the stem leaf plot should be computed.
- +vec2+: Optional secondary vector, will be used to create a back-to-back stem-leaf plot.
- +scale+: Set scale of plot. Default = 10. Scale is changed via orders of magnitude. Common values are 0.1, 1, and 10. For String stems, the default value of 10 is a one character stem, 100 is a two character stem.
-
+divider+: Character for break between stem and leaf. Default = “ ” - +padchar+: Character(s) to separate stems, leaves and dividers. Default = “ “
- +trim+: Trims the stem labels when there are no leaves. This can be useful if your data is sparse. Default = +false+
- +string_padchar+: Character used to replace missing position for input strings shorter than the stem-size. Default = “_”
Result
A plot of object type is sent to $stdout
Direct Known Subclasses
Class Method Summary collapse
-
.factory(vector, **kw) ⇒ NumericStemplot, StringStemplot
Factory method to create a Stemplot, creates either a NumericStemplot or StringStemplot depending on input.
Instance Method Summary collapse
-
#initialize(*_args, **_kw) ⇒ Stemplot
constructor
Use Stemplot.factory method – should not be directly called.
-
#insert(stem, leaf) ⇒ Object
Insert a stem and leaf.
-
#leaves(stem) ⇒ Array
Returns a list of leaves for a given stem.
-
#max_stem_length ⇒ Integer
Determines largest length of any stem.
-
#raw_stems ⇒ Array
Returns an unsorted list of stems.
-
#stems(all: true) ⇒ Array
Returns a sorted list of stems.
Constructor Details
Class Method Details
.factory(vector, **kw) ⇒ NumericStemplot, StringStemplot
Factory method to create a Stemplot, creates either a NumericStemplot
or StringStemplot depending on input.
94 95 96 97 98 99 100 101 |
# File 'src/lib/unicode_plot/stemplot.rb', line 94 def self.factory(vector, **kw) vec = Array(vector) if vec.all? { |item| item.is_a?(Numeric) } NumericStemplot.new(vec, **kw) else StringStemplot.new(vec, **kw) end end |
Instance Method Details
#insert(stem, leaf) ⇒ Object
Insert a stem and leaf
104 105 106 107 |
# File 'src/lib/unicode_plot/stemplot.rb', line 104 def insert(stem, leaf) @stemleafs[stem] ||= [] @stemleafs[stem] << leaf end |
#leaves(stem) ⇒ Array
Returns a list of leaves for a given stem
118 119 120 |
# File 'src/lib/unicode_plot/stemplot.rb', line 118 def leaves(stem) @stemleafs[stem] || [] end |
#max_stem_length ⇒ Integer
Determines largest length of any stem
124 125 126 |
# File 'src/lib/unicode_plot/stemplot.rb', line 124 def max_stem_length @stemleafs.values.map(&:length).max end |
#raw_stems ⇒ Array
Returns an unsorted list of stems
111 112 113 |
# File 'src/lib/unicode_plot/stemplot.rb', line 111 def raw_stems @stemleafs.keys end |
#stems(all: true) ⇒ Array
Returns a sorted list of stems
131 132 133 |
# File 'src/lib/unicode_plot/stemplot.rb', line 131 def stems(all: true) self.class.sorted_stem_list(raw_stems, all: all) end |