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

## 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(vector, **kw) click to toggle source

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
new(*_args, **_kw) click to toggle source

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(stem, leaf) click to toggle source

Insert a stem and leaf

# File lib/unicode_plot/stemplot.rb, line 104
def insert(stem, leaf)
  @stemleafs[stem] ||= []
  @stemleafs[stem] << leaf
end
leaves(stem) click to toggle source

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

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

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
stems(all: true) click to toggle source

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