Visualizing numerical data in Python: How to utilize Stem Plot
If you want to visualize numerical data over time or sequence, which graph is best? A Stem Plot is a clear Python visualization of change by representing data points as vertical lines and markers from a baseline to each value, as shown below.

In this post, we'll use the Python matplotlibto draw stem plots and effectively visualize numerical data. We'll provide detailed explanations with step-by-step code, so follow along!
Numerical data and stem plots
What is numerical data?
Numerical Data is a Numbersand is divided into continuous (height, weight) and discrete (count).
What is a stemplot?
Stemplots are created using the Graphs to visualize numerical data using vertical lines and markers.
- Data pointswith lines and markers.
- OrderI Changes over timewhen expressing a
Example of this data
Below are some real-world examples of Numerical data examples.

1. sensor measurements
The result of a sensor's measurement of temperature or humidity over time.
| Time (in minutes) | Temperature (°C) |
|---|---|
| 1 | 22 |
| 2 | 24 |
| 3 | 23 |
| 4 | 25 |
| … | … |
- UtilizationVisualize your time and temperature data as a stemplot to see patterns of temperature spikes or dips during certain times of day.
2. product sales volume
The number of times a product was sold over a specific time period (for example, per day).
| Date | Sales volume (pieces) |
|---|---|
| 1 day | 50 |
| 2 days | 80 |
| 3 days | 30 |
| 4 days | 100 |
| 5 days | 70 |
- Utilization: Stemplots allow you to intuitively see the fluctuations in sales by day, which can help you analyze what caused a spike in sales on a particular day.
3. test scores
The score students received on one test.
| Student number | Scores |
|---|---|
| 1 | 75 |
| 2 | 90 |
| 3 | 65 |
| 4 | 85 |
| … | … |
- UtilizationYou can use stemplots to highlight the distribution of scores and changes in performance for specific students.
4. the number of times the event occurred
Data that records the number of times an event occurred on a specific system or machine.
| Time Zone | Event count |
|---|---|
| 00:00 | 5 |
| 01:00 | 2 |
| 02:00 | 10 |
| 03:00 | 8 |
| … | … |
- Utilization: Seeing a pattern of events clustered around certain times of the day in a splot can help you analyze and optimize for causes.
Python stemplot code examples
Let's implement a stemplot to visualize numerical data with the code below.
import matplotlib.pyplot as plt
import numpy as np
Create # numerical data
x = np.range(0, 100, 1) # X-axis: ordered data from 0 to 99
y = np.random.randint(50, 300, size=100) # Y-axis: random numerical data between 50 and 300
Draw a # stemplot
plt.figure(figsize=(12, 6)) Set the # graph size
plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')
Title and label the # graph
plt.title("Stem Plot of Numerical Data", fontsize=15)
plt.xlabel("Index", fontsize=12)
plt.ylabel("Values", fontsize=12)
Tighten the # layout and graph output
plt.tight_layout()
plt.show()
Code briefs
- Generate data
x: A 0 through 99 number to use for the x-axis Sequential data.y: A number between 50 and 300 to use for the y-axis Random Numerical Data.
- Drawing a stemplot
plt.stem(): function to draw a stem plot.linefmt='b-': Show vertical lines as solid blue lines.markerfmt='bo': Mark the data point with a blue circular marker.basefmt='r-': The baseline (bottom line) is shown as a solid red line.
- Setting the graph layout
- Set the title, X/Y labels, and add the
tight_layout()to adjust the margins to output a cleaner graph.
- Set the title, X/Y labels, and add the
Finalize
Stem plots are useful for intuitive Python visualization of numerical data. They represent each point as a line and marker connected from a baseline, and can be used to create a Data changesin a clear way. You might consider the following use cases
Use cases
- Python visualization of sensor dataAnalyze changes in sensor values over time.
- Time series dataHighlight changes over a specific time period.
- Spike analysis: Detecting sudden fluctuations in data.
If you're interested in visualizing Python, Visualizing categorical data with Python visualizations: Grouped bar graphs and categorical data examples Check out the post and build your knowledge!
#Code verbose commentary
1. import the library
import matplotlib.pyplot as plt import numpy as np
matplotlib.pyplot: A library for drawing graphs.numpy: Used to generate numeric data.2. Generate numerical data
x = np.range(0, 100, 1) # X-axis: ordered data from 0 to 99 y = np.random.randint(50, 300, size=100) # Y-axis: random numeric data between 50 and 300
np.arange(0, 100, 1): Generates a number from 0 to 99 with an interval of 1.np.random.randint(50, 300, size=100): Generate 100 integers between 50 and 300.3. Draw a stem plot
plt.figure(figsize=(12, 6)) plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-', use_line_collection=True)
plt.figure(figsize=(12, 6)): Set the size of the graph to 12 across and 6 down.plt.stem(): Draw a stem plot.
linefmt='b-': Show vertical lines as solid blue lines.markerfmt='bo': Mark the data point with a blue circular marker.basefmt='r-': Show baseline as a solid red line.use_line_collection=TrueGraph performance optimization options.4. Set the graph title and label
plt.title("Stem Plot of Numerical Data", fontsize=15) plt.xlabel("Index", fontsize=12) plt.ylabel("Values", fontsize=12)
plt.title(): Sets the title of the graph.plt.xlabel(): Sets the X-axis label.plt.ylabel(): Sets the y-axis label.5. adjust the layout and output the graph
plt.tight_layout() plt.show()
plt.tight_layout(): Automatically adjusts the margins of the graph to prevent elements from overlapping.plt.show(): Prints the graph to the screen.




