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.

수치형 데이터 스템 플롯으로 파이썬 시각화한 그림
(Python visualization with numerical data stem plots)

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)
122
224
323
425
  • 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).

DateSales volume (pieces)
1 day50
2 days80
3 days30
4 days100
5 days70
  • 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 numberScores
175
290
365
485
  • 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 ZoneEvent count
00:005
01:002
02:0010
03:008
  • 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

  1. 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.
  2. 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.
  3. Setting the graph layout
    • Set the title, X/Y labels, and add the tight_layout()to adjust the margins to output a cleaner graph.

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.

Similar Posts