In this tutorial, we will learn about Stem and Leaf graphs and we will also look at their implementation. A
stem-and-leaf plot is a chart that displays data by splitting up each data value in the dataset into a stem and a leaf before plotting the points. A stem-and-leaf plot is also called a
Importance of Stem and Leaf Plots
Stem-and-leaf plots are useful for displaying the
relative density and help to give the reader a quick overview of the
distribution. They are also useful for highlighting outliers and finding the mode of the dataset.
Code Implementation for Stem and Leaf Plots in Python
We will be executing the following code snippets to create the plots for a dataset in Python. To create a stem-and-leaf plot for any dataset, we will make use of the
stemgraphic library. You can install the same using the statement below.
pip install stemgraphic
We will make use of
numpy module and the
random.randint function to get 20 numbers between 20 and 50.
import numpy as np data = np.random.randint(20, 50, 20) print(data)
When we execute the above code, we get the dataset as follows.
[33 28 21 22 37 45 23 45 36 24 20 45 43 24 45 21 20 43 21 47]
Next, we make use of the
stem_graphic function of the
stemgraphic module to automatically divide data into stems and leafs using the code below.
import stemgraphic fig, ax = stemgraphic.stem_graphic(data)
I bet you are not able to understand a thing about this plot and what exactly are you supposed to interpret from the plot. You can understand the interpretations from the following statements :
The red boxes display the minimum and the maximum number of the dataset where the bottom represents the minimum and the top represents the maximum.
The numbers in the far left display the
aggregated count of values in the plot. Also, the numbers in the middle column represent the
stems for the dataset. Lastly, the numbers in the far right column represent the
leaves of the dataset.
Another Illustration with the Complete Code
Let us have a look at another example and the complete code for the plotting of the stem-and-leaf plot in the Python programming language.
import stemgraphic import numpy as np data = np.random.randint(20, 50, 20) print(data) fig, ax = stemgraphic.stem_graphic(data)
I hope you loved reading the above tutorial about understanding what stem-and-leaf plots are along with a step-by-step guide to creating these plots. You can see how a single plot can provide tons of information about how the data is distributed in a dataset.
Thank you for reading! I would recommend you to read the following tutorials as well:
- Python diagrams module – Creating Architecture Diagrams
- Missingno – Visualize Missing Values In Python
- Python Plotly Tutorial
- Python Bokeh Data Visualization Tutorial