Class: UnicodePlot::Stemplot

Inherits:
Object
  • Object
show all
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

Examples:

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)

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: '?')

Direct Known Subclasses

NumericStemplot, StringStemplot

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args, **_kw) ⇒ Stemplot

Use factory method – should not be directly called.



84
85
86
# File 'src/lib/unicode_plot/stemplot.rb', line 84

def initialize(*_args, **_kw)
  @stemleafs = {}
end

Class Method Details

.factory(vector, **kw) ⇒ NumericStemplot, StringStemplot

Factory method to create a Stemplot, creates either a NumericStemplot
or StringStemplot depending on input.

Parameters:

  • vector (Array)

    An array of elements to stem-leaf plot

Returns:



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

Parameters:

  • stem (Object)

    The stem

Returns:

  • (Array)

    Unsorted list of leaves



118
119
120
# File 'src/lib/unicode_plot/stemplot.rb', line 118

def leaves(stem)
  @stemleafs[stem] || []
end

#max_stem_lengthInteger

Determines largest length of any stem

Returns:

  • (Integer)

    Length value



124
125
126
# File 'src/lib/unicode_plot/stemplot.rb', line 124

def max_stem_length
  @stemleafs.values.map(&:length).max
end

#raw_stemsArray

Returns an unsorted list of stems

Returns:

  • (Array)

    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

Parameters:

  • all (Boolean) (defaults to: true)

    Return all stems if true, otherwise only return stems if a leaf exists for a stem

Returns:

  • (Array)

    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