Stem and leaf areas in Python



Stem and leaf graph — it is a tabular view in which each data value is broken down into a “base” (first digit or numbers) and a “sheet” (usually the last digit).

Interpretations :

 "17" is split into "1" (stem) and "7" (leaf) "69" is split into "6" (stem) and "9" (leaf) 

The order of the stem-leaf section:

  1. Divide each observation / data into a stem that will consist of all but the rightmost digit and leaf, the rightmost digit.
  2. The leaf should only have one digit and the trunk can have as many digits as possible.
  3. Write the trunk in a vertical column with the smallest at the top (but in Python you will get the largest at the top), then draw a vertical line to the right from that column.
  4. Write each corresponding leaf in a row to the right of its stem just after the vertical line, in ascending order from the stem.

Example:

Let`s say Python.Engineering has 10 tech authors th content. Each of them submitted 100 articles to publish on the site. Out of 100 articles, the number of articles with errors is shown below for every 10 content authors —

 16, 25, 47, 56, 23, 45, 19, 55, 44, 27 

Stem-leaf area will be —

 1 | 69 2 | 357 4 | 457 5 | 56 

Plot in Python using the stem module —
To plot the stems and leaves in Python, we need to install the strong module & gt; stemgraphic. You can install stemgraphic module from Jupyter Notebook as —

 import sys! {Sys.executable} -m pip install stemgraphic 

Below is the code —

# module import

import stemgraphic

 

data = [ 16 , 25 , 47 , 56 , 23 , 45 , 19 , 55 , 44 , 27 ]

 
# call stem_graphic with required parameters,
# data and scale

stemgraphic.stem_graphic (data, scale = 10 )

Output:

Explanation —
The leftmost column in the above graph is — this is a frequency counter. There are two observations in the 10-20 range and 3 observations in the 20-30 range, which gives a total of 5 observations in the 0-30 range. Continuing in this vein, we see 10 observations that are at the top in the same column. Then, after the vertical line, there are two values, one of which we have at the most 16. Although at the topmost we have 56, these values ​​are nothing more than the minimum and maximum values, respectively, in the given dataset. After that we have the stem values, and after that we are left with the values ​​dividing the stem along a vertical line.

Note: You can set this Matplotlib a library from Jupyter Notebook like —

 import sys! {sys.executable} -m pip install matplotlib 

Using matplotlib.pyplot.stem

# import matplotlib.pyplot library

import matplotlib.pyplot as plt

 

data = [ 16 , 25 , 47 , 56 , 23 , 45 , 19 , 55 , 44 , 27 ]

 
# stem parts separation

stems = [ 1 , 1 , 2 , 2 , 2 , 4 , 4 , 4 , 5 , 5 ]

 

plt.ylabel ( `Data` # for Y-axis label

 

plt.xlabel ( `stems` # for labels on the x-axis

 

plt.xlim ( 0 , 10 # x-axis limit

 

plt.stem (stems, data)  # required section

Output:

Link: https://pypi.org/project/stemgraphic/