Matplotlib - Stem Plots



A stem plot is a type of graph used to represent data where each data point is shown as a marker along a vertical line, called a stem, extending from a horizontal axis. The marker represents the value of the data point, while the position along the horizontal axis represents its location or category.

Imagine you are conducting a survey to collect the number of cars passing through a particular road intersection each hour of the day. You want to visualize this data to understand the traffic patterns. You can create a stem plot in this case, such that each hour of the day is represented along the horizontal axis, while the number of cars passing through is indicated by the length of the stems extending vertically from the corresponding hour −

Stem Plots

Stem Plots in Matplotlib

A stem plot in Matplotlib is a visual representation of the frequency of data points along a vertical axis in two-dimensional space. We can use the stem() function within the 'pyplot' module to create a stem plot in Matplotlib. This function takes two parameters: the data point values defined by X and their frequency defined by Y. It then creates a vertical line between the X value and its corresponding Y value.

Lets start by drawing a basic stem plot.

Basic Stem Plot

A basic stem plot in Matplotlib is a simple way to visualize the frequency of data points along a vertical axis. The frequency of each data point is represented by a vertical line extending from the x-axis to the data value on the y-axis.

Example

In the following example, we are creating a basic stem plot. We use two arrays, 'x' and 'y', to represent the data points and their frequencies. Then, we use the stem() function to plot a vertical line at each x value extending to the corresponding y value −

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Creating a basic stem plot
plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Basic Stem Plot')
plt.show()

Output

Following is the output of the above code −

Basic Stem Plot

Horizontal Stem Plot

In Matplotlib, a horizontal stem plot is similar to a basic stem plot, but the stems are oriented horizontally instead of vertically. This means that the frequency of each data point is represented by a horizontal line extending from the y-axis to the data value on the x-axis.

Example

In here, we are creating a horizontal stem plot. We again use two arrays, 'x' and 'y', to represent the data points and their frequencies. Then, we use the stem() function with a horizontal orientation to make the vertical axis as the x-axis and the horizontal axis as the y-axis. In the resultant plot, horizontal lines extend from the x value to the y value −

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Creating a stem plot with horizontal alignment
plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-', orientation='horizontal')
plt.xlabel('Y')
plt.ylabel('X')
plt.title('Horizontal Stem Plot')
plt.show()

Output

Output of the above code is as follows −

Horizontal Stem Plot

Sinusoidal Stem Plot

A sinusoidal stem plot in Matplotlib a way to visually represent data points that follow a sinusoidal pattern.

Imagine data points distributed along a sinusoidal curve. Instead of connecting these points with straight lines, a stem plot places vertical stems at each data point, and the height of each stem corresponds to the value of the data point.

Example

The following example creates a sinusoidal stem plot in Matplotlib using the stem() function. We start by creating evenly distributed data points and then find their frequencies by taking the sine value of each data point. The resultant plot creates a stem plot in a continuous form −

import matplotlib.pyplot as plt
import numpy as np

# Generating sinusoidal data
x = np.linspace(0, 2*np.pi, 50)
y = np.sin(x)

# Creating a stem plot of sinusoidal data
plt.stem(x, y, linefmt='b-', markerfmt='bo', basefmt='r-')

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Sinusoidal Stem Plot')
plt.show()

Output

After executing the above code, we get the following output −

Sinusoidal Stem Plot

Triangular Stem Plot

A triangular stem plot in Matplotlib is a graphical representation where data points are shown as vertical stems rising from the x-axis, forming a triangular shape.

Each stem represents a data point, with its height indicating the value of the data point. Unlike a regular stem plot where stems are vertical lines, in a triangular stem plot, the stems have a triangular shape.

Example

Now, we are creating a triangular stem plot, where the leaf of the stem is a triangle instead of circle. We use arrays 'x' and 'y' to define the data points and their frequencies. To create a triangular leaf, we set the 'markerfmt' parameter (this parameter is used to define the shape of the leaves) to '^'. This results in a plot where the leaves of the steam are triangular −

import matplotlib.pyplot as plt

# Sample data
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]

# Creating a stem plot with triangular markers
plt.stem(x, y, linefmt='g-', markerfmt='^b', basefmt='r-')

plt.xlabel('X')
plt.ylabel('Y')
plt.title('Triangular Stem Plot')
plt.show()

Output

The output obtained is as shown below −

Triangular Stem Plot
Advertisements