class UnicodePlot::Stemplot
## 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
@example Examples using Numbers
# Generate some numbers fifty_floats = 50.times.map { rand(-1000..1000)/350.0 } eighty_ints = 80.times.map { rand(1..100) } another_eighty_ints = 80.times.map { rand(1..100) } three_hundred_ints = 300.times.map { rand(-100..100) } # Single sided stem-plot UnicodePlot.stemplot(eighty_ints) # Single sided stem-plot with positive and negative values UnicodePlot.stemplot(three_hundred_ints) # Single sided stem-plot using floating point values, scaled UnicodePlot.stemplot(fifty_floats, scale: 1) # Single sided stem-plot using floating point values, scaled with new divider UnicodePlot.stemplot(fifty_floats, scale: 1, divider: "😄") # Back to back stem-plot UnicodePlot.stemplot(eighty_ints, another_eighty_ints)
@example Examples using Strings
# Generate some strings words_1 = %w[apple junk ant age bee bar baz dog egg a] words_2 = %w[ape flan can cat juice elf gnome child fruit] # Single sided stem-plot UnicodePlot.stemplot(words_1) # Back to back stem-plot UnicodePlot.stemplot(words_1, words_2) # Scaled stem plot using scale=100 (two letters for the stem) and trimmed stems UnicodePlot.stemplot(words_1, scale: 100, trim: true) # Above, but changing the string_padchar UnicodePlot.stemplot(words_1, scale: 100, trim: true, string_padchar: '?')
Public Class Methods
Factory method to create a Stemplot
, creates either a NumericStemplot
or StringStemplot
depending on input.
@param vector [Array] An array of elements to stem-leaf plot @return [NumericStemplot] If all elements are Numeric @return [StringStemplot] If any elements are not Numeric
# File 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
Use {factory} method – should not be directly called.
# File lib/unicode_plot/stemplot.rb, line 84 def initialize(*_args, **_kw) @stemleafs = {} end
Public Instance Methods
Insert a stem and leaf
# File lib/unicode_plot/stemplot.rb, line 104 def insert(stem, leaf) @stemleafs[stem] ||= [] @stemleafs[stem] << leaf end
Returns a list of leaves for a given stem @param stem [Object] The stem @return [Array] Unsorted list of leaves
# File lib/unicode_plot/stemplot.rb, line 118 def leaves(stem) @stemleafs[stem] || [] end
Determines largest length of any stem @return [Integer] Length value
# File lib/unicode_plot/stemplot.rb, line 124 def max_stem_length @stemleafs.values.map(&:length).max end
Returns an unsorted list of stems @return [Array] Unsorted list of stems
# File lib/unicode_plot/stemplot.rb, line 111 def raw_stems @stemleafs.keys end
Returns a sorted list of stems @param all [Boolean] Return all stems if true, otherwise only return stems if a leaf exists for a stem @return [Array] Sorted list of stems
# File lib/unicode_plot/stemplot.rb, line 131 def stems(all: true) self.class.sorted_stem_list(raw_stems, all: all) end