Stem and Leaf Plots in Python
Last Updated :
09 May, 2019
Stem and Leaf Plot is a way of representing the data. This plot is used to show the absolute frequency in different classes similar to the frequency distribution table or a histogram. It presents the quantitative data in the graphical format, and the stem-and-leaf plot of quantitative data is said as textual graph as that presents the data according to their most significant numeric digit. Stem and Leaf Plot graph is mainly suitable for smaller data sets.
Stem-and-leaf plot is a tabular presentation where each data value is split into a “stem” (the first digit or digits) and a “leaf” (usually the last digit).
Interpretations:
"17" is split into "1" (stem) and "7" (leaf)
"69" is split into "6" (stem) and "9" (leaf)
Procedure to make stem-and-leaf plot:
- Separate each observation/data into a stem which will consist of all except rightmost digit and leaf, the rightmost digit.
- Leaf must have only one digit while stem can have as many digits as possible.
- Write the stem in a vertical column with smallest at the top(but in Python, you will get largest at the top) then draw a vertical line by the right of this column.
- Write each corresponding leaf in the row to the right of its stem just after the vertical line, in ascending order out from the stem.
Example:
Let’s say there are 10 Technical Content Writers at GeeksforGeeks. Each of them submitted 100 articles
to publish at the site. Out of 100 articles, the number of articles which had some errors are given below for each 10 content writers –
16, 25, 47, 56, 23, 45, 19, 55, 44, 27
Stem-and-leaf plot will be –
1 | 69
2 | 357
4 | 457
5 | 56
Plot in Python using stemgraphic module –
To plot stem-and-leaf plot in Python, we need to install the <strong>stemgraphic module
. You can install stemgraphic module from Jupyter Notebook as –
import sys
!{sys.executable} -m pip install stemgraphic
Below is the code –
import stemgraphic
data = [ 16 , 25 , 47 , 56 , 23 , 45 , 19 , 55 , 44 , 27 ]
stemgraphic.stem_graphic(data, scale = 10 )
|
Output:

Explanation –
The leftmost column in the above plot is the frequency count. There are two observations in the range 10-20 and 3 observations in the range 20-30, which gives total of 5 observations in the range 0-30. Continuing in the same way, there is total of 10 observations which is at the top in the same column. Then after a vertical line, there are two values, one at bottom most we have 16. While at the topmost we have 56, these values are nothing but the minimum and maximum values respectively in the given data set. After that, we have the stem values and following that we have leaves values separating stem by the vertical line.
Note: You can install this matplotlib library from Jupyter Notebook as –
import sys
!{sys.executable} -m pip install matplotlib
Using matplotlib.pyplot.stem
–
import matplotlib.pyplot as plt
data = [ 16 , 25 , 47 , 56 , 23 , 45 , 19 , 55 , 44 , 27 ]
stems = [ 1 , 1 , 2 , 2 , 2 , 4 , 4 , 4 , 5 , 5 ]
plt.ylabel( 'Data' )
plt.xlabel( 'stems' )
plt.xlim( 0 , 10 )
plt.stem(stems, data)
|
Output:

Reference: https://pypi.org/project/stemgraphic/
Similar Reads
Matplotlib.pyplot.stem() in Python
Matplotlib is a visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.pyplot.stem() matplotlib.pyplot.stem() creates stem plots. A Stem plot plots vertical
3 min read
Matplotlib.axes.Axes.stem() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
Surface plots and Contour plots in Python
Surface plots and contour plots are visualization tools used to represent three-dimensional data in two dimensions. They are commonly used in mathematics, engineering and data analysis to understand the relationships between three variables. In this article, we will understand about surface and cont
3 min read
3D Line Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
matplotlib.axes.Axes.step() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
2 min read
Carpet Plots using Plotly in Python
A Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. Plotly is an interactive visualization libra
3 min read
3D scatter plot using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
3 min read
3D Cone Plots using Plotly in Python
Plotly is a Python library that is used to design graphs, especially interactive graphs. It can plot various graphs and charts like histogram, barplot, boxplot, spreadplot, and many more. It is mainly used in data analysis as well as financial analysis. plotly is an interactive visualization library
2 min read
Graph Plotting in Python | Set 1
This series will introduce you to graphing in Python with Matplotlib, which is arguably the most popular graphing and data visualization library for Python.InstallationThe easiest way to install matplotlib is to use pip. Type the following command in the terminal:Â pip install matplotlibOR, you can
9 min read
Graph Plotting in Python | Set 2
Graph Plotting in Python | Set 1 Subplots Subplots are required when we want to show two or more plots in same figure. We can do it in two ways using two slightly different methods.Method 1 Python Code # importing required modules import matplotlib.pyplot as plt import numpy as np # function to gene
9 min read